I have a rather long string containing substrings with the following format:
project[1]/someword[1] project[1]/someotherword[1]
The line will contain about 10 instances of this template.
I want to do to replace the second integer in square brackets with another. So the line will look like this:
project[1]/someword[2] project[1]/someotherword[2]
I think regular expressions are what I need here. I came up with a regex:
project\[1\]/.*\[([0-9])\]
To do this, you need to capture the group [0-9], so that I can replace it with something else. I look at MSDN Regex.Replace (), but I donβt see how to replace the part of the string that is captured with the value of your choice. Any advice on how to do this would be appreciated. Thank you very much.
* Edit: * After working with @Tharwen, I changed my approach a bit. Here is the new code I'm working with:
String yourString = String yourString = @"<element w:xpath=""/project[1]/someword[1]""/> <anothernode></anothernode> <another element w:xpath=""/project[1]/someotherword[1]""/>"; int yourNumber = 2; string anotherString = string.Empty; anotherString = Regex.Replace(yourString, @"(?<=project\[1\]/.*\[)\d(?=\]"")", yourNumber.ToString());
source share