使用 regex_replace() 时,当格式字符串中紧跟另一个数字时,反向引用子匹配的正确方法是什么?
When using regex_replace(), what't the correct way to back-reference a submatch when it's directly followed by another digit in the format string?
在下面的代码中,我尝试使用
来引用第一个子匹配:
#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*)"}, "" + to_string(r));
cout << str << "\n";
return 0;
}
我期望的是:
1-2-1 4-5-1 7-8-1
但它不起作用,因为传递给 regex_replace()
的实际格式字符串是
,就好像我试图引用第 11 个子匹配一样。
那么当使用 regex_replace()
时,反向引用格式字符串中紧跟另一个数字的子匹配的正确方法是什么?
我尝试使用
,但它对我尝试过的任何主流实现都不起作用。
根据标准 N3337,§28.5.2,Table 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,Table 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.
所以,$
后面最多只能有两位小数,表示匹配组,所以可以用
"" + to_string(r)
在下面的代码中,我尝试使用 来引用第一个子匹配:
#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*)"}, "" + to_string(r));
cout << str << "\n";
return 0;
}
我期望的是:
1-2-1 4-5-1 7-8-1
但它不起作用,因为传递给 regex_replace()
的实际格式字符串是 ,就好像我试图引用第 11 个子匹配一样。
那么当使用 regex_replace()
时,反向引用格式字符串中紧跟另一个数字的子匹配的正确方法是什么?
我尝试使用 ,但它对我尝试过的任何主流实现都不起作用。
根据标准 N3337,§28.5.2,Table 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,Table 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.
所以,$
后面最多只能有两位小数,表示匹配组,所以可以用
"" + to_string(r)