TheMethod
TheMethod

Reputation: 3000

.NET regex replace using backreference

I have a fairly long string that contains sub strings with the following format:

project[1]/someword[1]
project[1]/someotherword[1]

There will be about 10 or so instances of this pattern in the string.

What I want to do is to be able to replace the second integer in square brackets with a different one. So the string would look like this for instance:

project[1]/someword[2]
project[1]/someotherword[2]

I''m thinking that regular expressions are what I need here. I came up with the regex:

project\[1\]/.*\[([0-9])\]

Which should capture the group [0-9] so I can replace it with something else. I'm looking at MSDN Regex.Replace() but I'm not seeing how to replace part of a string that is captured with a value of your choosing. Any advice on how to accomplish this would be appreciated. Thanks much.

*Edit: * After working with @Tharwen some I have changed my approach a bit. Here is the new code I am 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());

Upvotes: 7

Views: 10489

Answers (4)

Tharwen
Tharwen

Reputation: 3107

I've adapted yours to use a lookbehind and lookahead to only match a digit which is preceded by 'project[1]/xxxxx[' and followed by ']':

(?<=project\[1\]/.*\[)\d(?=\]")

Then, you can use:

String yourString = "project[1]/someword[1]";
int yourNumber = 2;
yourString = Regex.Replace(yourString, @"(?<=project\[1\]/.*\[)\d(?=\]"")", yourNumber.ToString());

I think maybe you were confused because Regex.Replace has lots of overloads which do slightly different things. I've used this one.

Upvotes: 2

James Kyburz
James Kyburz

Reputation: 14503

Matched groups are replaced using the $1, $2 syntax as follows :-

csharp> Regex.Replace("Meaning of life is 42", @"([^\d]*)(\d+)", "$1($2)");
"Meaning of life is (42)"

If you are new to regular expressions in .NET I recommend http://www.ultrapico.com/Expresso.htm

Also http://www.regular-expressions.info/dotnet.html has some good stuff for quick reference.

Upvotes: 32

Jules
Jules

Reputation: 559

If you want to process the value of a captured group before replacing it, you'll have to separate the different parts of the string, make your modifications and put them back together.

string test = "project[1]/someword[1]\nproject[1]/someotherword[1]\n";

string result = string.Empty;
foreach (Match match in Regex.Matches(test, @"(project\[1\]/.*\[)([0-9])(\]\n)"))
{
    result += match.Groups[1].Value;
    result += (int.Parse(match.Groups[2].Value) + 1).ToString();
    result += match.Groups[3].Value;
}

If you just want to replace text verbatim, it's easier: Regex.Replace(test, @"abc(.*)cba", @"cba$1abc").

Upvotes: 0

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

you can use String.Replace (String, String) for example

String.Replace ("someword[1]", "someword[2]")

Upvotes: -3

Related Questions