gpt4 book ai didi

c++ - 使用 regex_replace() 时,当格式字符串中紧跟另一个数字时,反向引用子匹配的正确方法是什么?

转载 作者:行者123 更新时间:2023-11-30 03:57:48 25 4
gpt4 key购买 nike

在下面的代码中,我尝试使用$1 来引用第一个子匹配:

#include <iostream>
#include <string>
#include <regex>

using namespace std;

int main()
{
string str {"1-2-3 4-5-6 7-8-9"};
int r = 1;
str = regex_replace(str, regex{R"((\d*-\d*-)\d*)"}, "$1" + to_string(r));
cout << str << "\n";
return 0;
}

我期望的是:

1-2-1 4-5-1 7-8-1

但它不起作用,因为传递给 regex_replace() 的实际格式字符串是 $11,就好像我试图引用第 11 个子匹配一样。

那么在使用 regex_replace() 时,反向引用格式字符串中紧跟另一个数字的子匹配的正确方法是什么?

我尝试使用 ${1},但它对我尝试过的任何主流实现都不起作用。

最佳答案

根据标准 N3337,§28.5.2,表 139:

format_default: When a regular expression match is to be replaced by a new string, the new string shall be constructed using the rules used by the ECMAScript replace function in ECMA-262, part 15.5.4.11 String.prototype.replace. In addition, during search and replace operations all non-overlapping occurrences of the regular expression shall be located and replaced, and sections of the input that did not match the expression shall be copied unchanged to the output string.

并根据ECMA-262 part 15.5.4.11 String.prototype.replace , 表 22

$nn: The nn-th capture, where nn is a two-digit decimal number in the range 01 to 99. If nn≤m and the nnth capture is undefined, use the empty String instead. If nn>m, the result is implementation-defined.

所以$后面最多只能有两位小数,表示匹配组,所以可以用

"$01" + to_string(r)

关于c++ - 使用 regex_replace() 时,当格式字符串中紧跟另一个数字时,反向引用子匹配的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27783017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com