jeudi 28 juin 2018

regex_replace, why does it lost the $1?

string s = " 'I'd go.' ";
s = std::regex_replace(s, std::regex("((^| )')|('($| ))"), "$1(Quotation, )");
cout << s; // '(Quotation, )I'd go.(Quotation, )

I want to replace the ' with (Quotation, ), and I don't want to lose the original '. So, I use $1 to mean the original '. And I don't want to replace the ' of I'd.

^ means if the ' is the start of the string it would be replaced. $ means the end of the string.

The result is supposed to be

'(Quotation, )I'd go.' (Quotation, )

But actually the result is

'(Quotation, )I'd go.(Quotation, )

The left quotation replacement works fine, but the right loses the ' . Why?

Aucun commentaire:

Enregistrer un commentaire