首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::regex_replace

Defined in header <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>& re, const std::basic_string<CharT,STraits,SAlloc>& fmt, std::regex_constants::match_flag_type flags = std::regex_constants::match_default );

(1)

(since 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>& re, const CharT* fmt, std::regex_constants::match_flag_type flags = std::regex_constants::match_default );

(2)

(since 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>& re, const std::basic_string<CharT,FTraits,FAlloc>& fmt, std::regex_constants::match_flag_type flags = std::regex_constants::match_default );

(3)

(since 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>& re, const CharT* fmt, std::regex_constants::match_flag_type flags = std::regex_constants::match_default );

(4)

(since 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>& re, const std::basic_string<CharT,STraits,SAlloc>& fmt, std::regex_constants::match_flag_type flags = std::regex_constants::match_default );

(5)

(since C++11)

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

(6)

(since C++11)

regex_replace使用正则表达式对字符序列执行替换:

1%个拷贝范围内的29个字符[first,last)out,替换任何匹配的序列。re格式化的字符fmt.换言之:

  • 构造一个std::regex_iterator对象i好像std::regex_iterator<BidirIt, CharT, traits> i(first, last, re, flags),并使用它来逐步完成每一场比赛。re在序列内[first,last)...
  • 每一场比赛m,复制不匹配的子序列%28。m.prefix()29%out好像out =std::copy(m.prefix().first, m.prefix().second, out)然后用格式化的替换字符串替换匹配的子序列,就好像通过调用out = m.format(out, fmt, flags)...
  • 当没有找到匹配的字符时,将剩余的不匹配字符复制到out好像out =std::copy(last_m.suffix().first, last_m.suffix().second, out)何地last_m是最后找到的匹配的副本。
  • 如果没有匹配,则将整个序列复制到out就像现在一样out =std::copy(first, last, out)
  • 如果flagsstd::regex_constants::format_no_copy,则不将不匹配的子序列复制到out...
  • 如果flagsstd::regex_constants::format_first_only,只有第一次匹配被替换。

2%29与1%29相同,但格式化的替换是通过调用out = m.format(out, fmt, fmt + char_traits<charT>::length(fmt), flags)

3-4%29构造空字符串。result类型std::basic_string<CharT, ST, SA>和电话std::regex_replace(std::back_inserter(result), s.begin(), s.end(), re, fmt, flags)...

5-6%29构造空字符串。result类型std::basic_string<CharT>和电话std::regex_replace(std::back_inserter(result), s, s +std::char_traits<CharT>::length(s), re, fmt, flags)...

参数

first, last

-

the input character sequence, represented as a pair of iterators

s

-

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

re

-

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

flags

-

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

fmt

-

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

out

-

output iterator to store the result of the replacement

类型要求

-输出必须符合输出器的要求。

-Bidirit必须符合双向迭代器的要求。

返回值

1-2%29返回输出迭代器的副本。out在所有的插入之后。

3-6%29返回字符串。result其中包含输出。

例外

可抛std::regex_error表示误差条件...

二次

代码语言:javascript
复制
#include <iostream>
#include <iterator>
#include <regex>
#include <string>
 
int main()
{
   std::string text = "Quick brown fox";
   std::regex vowel_re("a|e|i|o|u");
 
   // write the results to an output iterator
   std::regex_replace(std::ostreambuf_iterator<char>(std::cout),
                      text.begin(), text.end(), vowel_re, "*");
 
   // construct a string holding the results
   std::cout << '\n' << std::regex_replace(text, vowel_re, "[$&]") << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
Q**ck br*wn f*x
Q[u][i]ck br[o]wn f[o]x

二次

另见

regex_search (C++11)

attempts to match a regular expression to any part of a character sequence (function template)

match_flag_type (C++11)

options specific to matching (typedef)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com