Replacement to empty from ^ moves ^ matching to the new position

81 Views Asked by At

I'm trying to remove the first 0 and the last 1 in the string 00211, but instead of 021 I get 21:

String temp = "00211";
TRegEx RegExp;
temp = RegExp.Replace(temp, "^0|1$", "", (TRegExOptions)roNone);
ShowMessage(temp); // "21", but I expect "021"

A similar issue with the expression ^. - instead of removing the first char only, it drops them all.

How can I fix this? I'm using C++Builder 10.2.3 Tokyo.


Same code in Javascript works fine:

console.log("00211".replace(/^0|1$/g, ""))

And in C++, if I use g++ and std::regex, it works:

cout << regex_replace("00211", regex("^0|1$"), ""); // "021"
0

There are 0 best solutions below