后缀 ($') 在 C++ 中不起作用regex_replace

Suffix ($´) in c++ regex_replace doesn't work

本文关键字:C++ 不起作用 regex replace 后缀      更新时间:2023-10-16

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

#include <iostream>
#include <string>
#include <regex>
#include <iterator>
int main ()
{
  std::string s ("there is a subsequence in the stringn");
  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规范页面复制)

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

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上那个页面的制作者只是打字错误。

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

正确的字符是

$`  prefix
$&  matched characters
$'  suffix

这是一个替代参考。