◐ Shell
clean mode source ↗

std::regex_replace — cppreference.com

Материал из cppreference.com

<metanoindex/>

<tbody> </tbody>

Определено в заголовочном файле <regex>

template< class OutputIt, class BidirIt, class Traits, class CharT, class STraits, class SAlloc > OutputIt regex_replace( OutputIt out, BidirIt first, BidirIt last, const std::basic_regex<CharT,Traits>& e, const std::basic_string<CharT,STraits,SAlloc>& fmt, std::regex_constants::match_flag_type flags = std::regex_constants::match_default );

(1) (начиная с C++11)

template< class OutputIt, class BidirIt, class Traits, class CharT > OutputIt regex_replace( OutputIt out, BidirIt first, BidirIt last, const std::basic_regex<CharT,Traits>& e, const CharT* fmt, std::regex_constants::match_flag_type flags = std::regex_constants::match_default );

(2) (начиная с C++11)

template< class Traits, class CharT, class STraits, class SAlloc, class FTraits, class FAlloc > std::basic_string<CharT,STraits,SAlloc> regex_replace( const std::basic_string<CharT,STraits,SAlloc>& s, const std::basic_regex<CharT,Traits>& e, const std::basic_string<CharT,FTraits,FAlloc>& fmt, std::regex_constants::match_flag_type flags = std::regex_constants::match_default );

(3) (начиная с C++11)

template< class Traits, class CharT, class STraits, class SAlloc > std::basic_string<CharT,STraits,SAlloc> regex_replace( const std::basic_string<CharT,STraits,SAlloc>& s, const std::basic_regex<CharT,Traits>& e, const CharT* fmt, std::regex_constants::match_flag_type flags = std::regex_constants::match_default );

(4) (начиная с C++11)

template< class Traits, class CharT, class STraits, class SAlloc > std::basic_string<CharT> regex_replace( const CharT* s, const std::basic_regex<CharT,Traits>& e, const std::basic_string<CharT,STraits,SAlloc>& fmt, std::regex_constants::match_flag_type flags = std::regex_constants::match_default );

(5) (начиная с C++11)

template< class Traits, class CharT > std::basic_string<CharT> regex_replace( const CharT* s, const std::basic_regex<CharT,Traits>& e, const CharT* fmt, std::regex_constants::match_flag_type flags = std::regex_constants::match_default );

(6) (начиная с C++11)

1)

Создает объект std::regex_iterator i как бы std::regex_iterator<BidirIt, CharT, traits> i(first, last, e, flags), и использует его для пошагового каждый матч e в последовательности [first,last). Для каждого такого m матч, копирует не соответствием подпоследовательность (m.prefix()) в out как есть, а затем заменяет соответствует последовательности с отформатированную строку замены как при вызове m.format(out, fmt, flags). Когда больше нет совпадения найдены, копирует оставшиеся не соответствием символов out.

Оригинал:

Constructs a std::regex_iterator object i as if by std::regex_iterator<BidirIt, CharT, traits> i(first, last, e, flags), and uses it to step through every match of e within the sequence [first,last). For each such match m, copies the non-matched subsequence (m.prefix()) into out as-is and then replaces the matched subsequence with the formatted replacement string as if by calling m.format(out, fmt, flags). When no more matches are found, copies the remaining non-matched characters to out.

Текст был переведён автоматически используя Переводчик Google.
Вы можете проверить и исправить перевод. Для инструкций щёлкните сюда.

Если совпадений нет, копирует всю последовательность в out как есть.

Оригинал:

If there are no matches, copies the entire sequence into out as-is.

Текст был переведён автоматически используя Переводчик Google.
Вы можете проверить и исправить перевод. Для инструкций щёлкните сюда.

Если flags содержит std::regex_constants::format_no_copy, не соответствием подпоследовательности не копируются в out.

Оригинал:

If flags contains std::regex_constants::format_no_copy, the non-matched subsequences are not copied into out.

Текст был переведён автоматически используя Переводчик Google.
Вы можете проверить и исправить перевод. Для инструкций щёлкните сюда.

Если flags содержит std::regex_constants::format_first_only, только первый матч заменить.

Оригинал:

If flags contains std::regex_constants::format_first_only, only the first match is replaced.

Текст был переведён автоматически используя Переводчик Google.
Вы можете проверить и исправить перевод. Для инструкций щёлкните сюда.

2)

же, как и 1), но отформатированные замена осуществляется как при вызове m.format(out, fmt, fmt + char_traits<charT>::length(fmt), flags)

Оригинал:

same as 1), but the formatted replacement is performed as if by calling m.format(out, fmt, fmt + char_traits<charT>::length(fmt), flags)

Текст был переведён автоматически используя Переводчик Google.
Вы можете проверить и исправить перевод. Для инструкций щёлкните сюда.

3-4)

Создает пустую строку result типа std::basic_string<CharT, ST, SA> и вызывает std::regex_replace(std::back_inserter(result), s.begin(), s.end(), e, fmt, flags).

Оригинал:

Constructs an empty string result of type std::basic_string<CharT, ST, SA> and calls std::regex_replace(std::back_inserter(result), s.begin(), s.end(), e, fmt, flags).

Текст был переведён автоматически используя Переводчик Google.
Вы можете проверить и исправить перевод. Для инструкций щёлкните сюда.

5-6)

Создает пустую строку result типа std::basic_string<CharT> и вызывает std::regex_replace(std::back_inserter(result), s, s + std::char_traits<CharT>::length(s), e, fmt, flags).

Оригинал:

Constructs an empty string result of type std::basic_string<CharT> and calls std::regex_replace(std::back_inserter(result), s, s + std::char_traits<CharT>::length(s), e, fmt, flags).

Текст был переведён автоматически используя Переводчик Google.
Вы можете проверить и исправить перевод. Для инструкций щёлкните сюда.

Параметры

first, last

входная последовательность символов, представленных в виде пары итераторов

Оригинал:

the input character sequence, represented as a pair of iterators

Текст был переведён автоматически используя Переводчик Google.
Вы можете проверить и исправить перевод. Для инструкций щёлкните сюда.

s

входная последовательность символов, представленных в виде STD :: basic_string или массив символов

Оригинал:

the input character sequence, represented as std::basic_string or character array

Текст был переведён автоматически используя Переводчик Google.
Вы можете проверить и исправить перевод. Для инструкций щёлкните сюда.

e

std::basic_regex, которые будут сопоставляться с входной последовательности

Оригинал:

the std::basic_regex that will be matched against the input sequence

Текст был переведён автоматически используя Переводчик Google.
Вы можете проверить и исправить перевод. Для инструкций щёлкните сюда.

flags

Матч флагов типа std::regex_constants::match_flag_type

Оригинал:

the match flags of type std::regex_constants::match_flag_type

Текст был переведён автоматически используя Переводчик Google.
Вы можете проверить и исправить перевод. Для инструкций щёлкните сюда.

fmt

Формат регулярных выражений замены строки, синтаксис, зависит от величины flags

Оригинал:

the regex replacement format string, exact syntax depends on the value of flags

Текст был переведён автоматически используя Переводчик Google.
Вы можете проверить и исправить перевод. Для инструкций щёлкните сюда.

out

Выход итератора для хранения результате замены

Оригинал:

output iterator to store the result of the replacement

Текст был переведён автоматически используя Переводчик Google.
Вы можете проверить и исправить перевод. Для инструкций щёлкните сюда.

Требования к типам
-OutputIt должен соответствовать требованиям OutputIterator.
-BidirIt должен соответствовать требованиям BidirectionalIterator.

Возвращаемое значение

1-2)

Возвращает копию итератора вывода out.

Оригинал:

Returns a copy of the output iterator out.

Текст был переведён автоматически используя Переводчик Google.
Вы можете проверить и исправить перевод. Для инструкций щёлкните сюда.

3-6)

Возвращает строку, которая содержит result выходных.

Оригинал:

Returns the string result which contains the output.

Текст был переведён автоматически используя Переводчик Google.
Вы можете проверить и исправить перевод. Для инструкций щёлкните сюда.

Исключения

Может бросить std::regex_error, чтобы указать состояние ошибки.

Оригинал:

Текст был переведён автоматически используя Переводчик Google.
Вы можете проверить и исправить перевод. Для инструкций щёлкните сюда.

Пример

#include <iostream>
#include <regex>
#include <string>
int main()
{
   std::string text = "Quick brown fox";
   std::regex vowel_re("a|o|e|u|i");
   std::cout << std::regex_replace(text, vowel_re, "[$&]") << '\n';
}

Вывод:

См. также