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
是的flags
包含std::regex_constants::format_no_copy,不会被复制到不匹配的子序列out
.flags
包含std::regex_constants::format_first_only,只有第一场比赛被替换.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 | - | |
s | - | |
e | - | |
flags | - | |
fmt | - | |
out | - | |
类型要求 | ||
-OutputIt 必须满足 OutputIterator 的要求。
| ||
-BidirIt 必须满足 BidirectionalIterator 的要求。
|
[编辑] 返回值
[编辑] 例外
[编辑] 示例
#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) |
试图匹配正则表达式到字符序列的任何部分 (函数模板) |
(C++11) |
特定于匹配的选项 (typedef) |