regex_replace
描述 (Description)
它使目标序列(主题)的副本与正则表达式rgx(模式)的所有匹配替换为fmt(替换)。 目标序列是s或第一个和最后一个之间的字符序列,具体取决于使用的版本。
声明 (Declaration)
以下是std :: regex_replace的声明。
template <class traits, class charT>
basic_string<charT>regex_replace (const charT* s,
const basic_regex<charT,traits>& rgx,
const charT* fmt,
C++11
template <class traits, class charT>
basic_string<charT>regex_replace (const charT* s,
const basic_regex<charT,traits>& rgx,
const charT* fmt,
C++14
template <class traits, class charT>
basic_string<charT>regex_replace (const charT* s,
const basic_regex<charT,traits>& rgx,
const charT* fmt,
参数 (Parameters)
s - 它是一个包含目标序列的字符串。
rgx - 它是一个匹配的basic_regex对象。
flags - 用于控制rgx的匹配方式。
m - 它是match_results类型的对象。
返回值 (Return Value)
它返回一个带有结果序列的字符串对象。
异常 (Exceptions)
No-noexcept - 这个成员函数永远不会抛出异常。
例子 (Example)
在下面的示例中为std :: regex_replace。
#include <iostream>
#include <string>
#include <regex>
#include <iterator>
int main () {
std::string s ("there is a subsequence in the string\n");
std::regex e ("\\b(sub)([^ ]*)");
std::cout << std::regex_replace (s,e,"sub-$2");
std::string result;
std::regex_replace (std::back_inserter(result), s.begin(), s.end(), e, "$2");
std::cout << result;
std::cout << std::regex_replace (s,e,"$1 and $2",std::regex_constants::format_no_copy);
std::cout << std::endl;
return 0;
}
输出应该是这样的 -
there is a sub-sequence in the string
there is a sequence in the string
sub and sequence