c ++ regex_replace中的后缀($')不起作用

时间:2015-10-29 09:39:15

标签: c++ c++11

根据c++ regex_replace specification应该指定匹配的后缀。但它不起作用:

#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(a )(subsequence)(.*)");
  // with flags:
  std::cout << std::regex_replace (s,e,"$´1digit$03",std::regex_constants::format_default);
  std::cout << std::endl;

  return 0;
}

输出:

there is $´1digit in the string

而不是后缀,它按字面打印。我怎样才能做到这一点?

N.B:我在键盘上找不到这个´字符(从cplusplus.com的regex_replace specification page复制)

2 个答案:

答案 0 :(得分:1)

它应该是一个简单的撇号'

std::cout << std::regex_replace (s,e,"$'1digit$03",std::regex_constants::format_default)

打印:

    v--the unmatched part
    v           v------the $' part (note that it includes a \n)
/------\  /-------------\
there is  in the string
1digit in the string
\__________________/
           ^---- the 1digit$03 part

因此,最有可能在cpluspluc.com上制作该页面的人只是打错了。

答案 1 :(得分:1)

cplusplus.com似乎存在一些印刷问题。 : - (

正确的字符是

$`  prefix
$&  matched characters
$'  suffix

这里是alternative reference