std::regex_replace
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <regex> 中定义
|
||
template< class OutputIt, class BidirIt, class Traits, class CharT, |
(1) | (C++11 起) |
template< class OutputIt, class BidirIt, class Traits, class CharT > |
(2) | (C++11 起) |
template< class Traits, class CharT, class STraits, class SAlloc, |
(3) | (C++11 起) |
template< class Traits, class CharT, class STraits, class SAlloc > |
(4) | (C++11 起) |
template< class Traits, class CharT, class STraits, class SAlloc > |
(5) | (C++11 起) |
template< class Traits, class CharT > std::basic_string<CharT> |
(6) | (C++11 起) |
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
.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
.out
是的out
as-is.flags
包含std::regex_constants::format_no_copy,不会被复制到不匹配的子序列out
.flags
contains std::regex_constants::format_no_copy, the non-matched subsequences are not copied into out
.flags
包含std::regex_constants::format_first_only,只有第一场比赛被替换.flags
contains std::regex_constants::format_first_only, only the first match is replaced.result
类型std::basic_string<CharT, ST, SA>构造一个空字符串,并调用std::regex_replace(std::back_inserter(result), s.begin(), s.end(), e, fmt, flags).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).result
类型std::basic_string<CharT>构造一个空字符串,并调用std::regex_replace(std::back_inserter(result), s, s + std::char_traits<CharT>::length(s), e, fmt, flags).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).目录 |
[编辑] 参数
first, last | - | 输入字符序列,表示为一对迭代器
原文: the input character sequence, represented as a pair of iterators |
s | - | 输入字符序列,如std :: basic_string的字符数组表示
原文: the input character sequence, represented as std::basic_string or character array |
e | - | 的std ::的basic_regex,将输入序列进行匹配
原文: the std::basic_regex that will be matched against the input sequence |
flags | - | 匹配标志的类型std::regex_constants::match_flag_type
原文: the match flags of type std::regex_constants::match_flag_type |
fmt | - | 正则表达式替换格式字符串,确切语法取决于
flags 的价值 原文: the regex replacement format string, exact syntax depends on the value of flags |
out | - | 输出迭代器来存储结果的更换
原文: output iterator to store the result of the replacement |
类型要求 | ||
-OutputIt 必须满足 OutputIterator 的要求。
| ||
-BidirIt 必须满足 BidirectionalIterator 的要求。
|
[编辑] 返回值
out
.out
.result
包含输出.result
which contains the output.[编辑] 例外
[编辑] 示例
#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'; }
输出:
Q[u][i]ck br[o]wn f[o]x
[编辑] 另请参阅
(C++11) |
尝试匹配正则表达式的字符序列的任何部分 原文: attempts to match a regular expression to any part of the character sequence (函数模板) |
(C++11) |
特定匹配的选项 (typedef) |