C# Regex replace using backreference

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


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:


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


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());


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 ']':


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.

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.

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").

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

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

Need Your Help

How to draw a filled circle in Java?

java user-interface swing graphics geometry

I have a JPanel with a Grid Layout. In the "cells" of the grid I can put different elements (for example JButtons). There is no problems with that. But now I want to put a filled circle in some of ...

How to enable iPod controls in the background to control non-iPod music in iOS 4?

iphone audio ios4 ipod mpmusicplayercontroller

A good example of what I'm trying to accomplish is implemented in the latest version of the Spotify iPhone application for (Pandora seems to have the same feature) .