I’m trying to replace a single character in a large string (textfile) using C#.
this string contain multiple lines of code.
At some point a batch file is called assigning multiple parameters:
call c:\script.bat 1 1 16 localhost 1 1 %0%
It must become:
call c:\script.bat 2 1 16 localhost 1 1 %0%
I’ve created the following statement to replace the regex dot group (in this case the first):
Regex.Replace(content, @"call c:\script.bat )(.)( 1 16 localhost 1 )(.)(%0%)","$1.$3$4$5")
Somehow I’m sure, replacing the number 1 that is in place of the match can’t be replaced with a 2 because then the replace string wil acces the non existing group 12 instead of group 1 with the character 2 attached to it.
Can someone give me a clue?
The simple solution would be to not capture the delimiting spaces:
Note: I’ve also used the
\ddigit character set. You may want to change this to\d+for numbers larger than a single digit.