Here's the code:
string myVar = "00000";
string myPtrn = "(.).(...)";
string mySub = "$1" + "1" + "$2";
string myResult = Regex.Replace(myVar, myPtrn, mySub);
MessageBox.Show("Before :\t" + myVar + "\nAfter :\t" + myResult);
The result is $11000
.
I'd like to have 01000
from 00000
.
But, I guess, $1
is confused with $11
.
You can put capturing group number inside {}
to avoid any confusion for regex engine like
string mySub = "${1}" + "1" + "$2";
Ideone Demo
As suggested in comments, you can also use
string mySub = "${1}1$2";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With