In the following code, I tried to use $1
to refer to the first submatch:
#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;
}
What I expect is:
1-2-1 4-5-1 7-8-1
But it doesn't work because the actual format string passed to regex_replace()
is $11
as if I were trying to refer to the 11th submatch.
So when using regex_replace()
, what is the correct way to back-reference a submatch which is followed directly by another digit in the format string?
I tried using ${1}
but it didn't work for every mainstream implementation that I tried.
Aucun commentaire:
Enregistrer un commentaire