Erick
Erick

Reputation: 6089

How to use a number beside a group number in regex replace

The title of the question is not exactly clear but the problem is quite simple.

I match the AssemblyVersion in an AssemblyInfo.cs file and try to replace the version.

Basically the code is similar to :

// FileContent is the text content of
// the AssemblyInfo.cs file
var version = new Version(2, 0, 0, 0);
var regex = new Regex(@"^(\s?\[assembly: AssemblyVersion\("")([0-9\.]+)(""\)])", RegexOption.Multiline);
var result = regex.Replace(FileContent, "$1" + version + "$3");

With this sample, result here equals $12.0.0.0")] which is not exactly a good result.

The only way I found to differenciate the $1 from the version number is to put a space, but it's not what I'm looking for.

How would I be able to resolve this ?

Upvotes: 1

Views: 43

Answers (1)

rslite
rslite

Reputation: 84713

You should be able to use ${1}. There are plenty of similar questions on SO (Why is "$1" ending up in my Regex.Replace() result?)

Upvotes: 1

Related Questions