From reading the FCD for regex_replace (28.11.4) I can only guess that the function can also use parts of the original string for replacing? I can not test it with my gcc, is this correct?
using namespace std;
regex rx{ R"((\d+)-(\d+))" }; // regex: (\d+)-(\d+)
cout << regex_replace("123-456", rx, "b: $2, a:$1");
// "b: 456, a:123"
As you can see, I assume $1 and $2 refer to the “()” capturing groups (and not \1 and \2 like elsewhere).
Update. So, I guess this is a two-part question
- Is this use of capturing groups in the replacement text supported at all?
- Is the default ECMAScript syntax using
$n? Or\n?
Table 139 in the C++ 2011 FDIS lists two constants that can be used to affect the rules used for the format string in
regex_replace,format_defaultandformat_sed.format_defaultis described as using “the rules used by the ECMAScript replace function in ECMA-262, part 15.5.4.11 String.prototype.replace.” This standard does indicate the use of$for backreferences. See: ECMA-262Using the
format_sedflag instead uses the rules for the sed utility in POSIX. Sed doesn’t appear to support$backreferences.