.NET Regex Expression For Finding & Replacing Any ProductName Value In A .vdproj File In C#

StackOverflow https://stackoverflow.com/questions/1817108

  •  08-07-2019
  •  | 
  •  

Question

I'm trying to write a NAnt extension task that can update different settings within a Visual Studio 2003 generated Setup .vdproj file, and would appreciate help with the following.

Specifically, I would like to use a RegEx expression to find, and if found, replace any value string value assigned to the ProductName value with a new string value in its entirty.

I am looking for a RegEx expression set to change "ProductName" to any other value without having to rely on anything other than that the string sought begins with "ProductName" = "8": and then has 1 or more characters and ends with a " mark. I've tried the following to no avail:

Before executing the following code fragment, the .vdproj file's ProductName reads:

"ProductName" = "8:My Simple .NET Application"

... and the code fragment in C#:

string _theProductName = "My Other Native Application";

Regex productNameExpression = new Regex( @"(?:\""ProductName\"" = \""8:*)" );

_theProjectFileContents = 
productNameExpression.Replace(_theProjectFileContents, 
                              "\"ProductName\" = \"8:" + _theProductName + "\"" );

bool updatedProductName = 
(_theProjectFileContents.IndexOf(_theProductName) >= 0);

After executing the above code fragment, the .vdproj file's ProductName now reads:

"ProductName" = "8:My Simple .NET Application"My Other Native Application"

Close, but I was expecting "My Other Native Application" to replace "My Simple .NET Application", and not add to it.

Any insights and help would be greatly appreciated.

Was it helpful?

Solution

You're very nearly there - only missing a single .
Change your regex thus and happiness should follow...

Regex productNameExpression = new Regex(@"(?:\""ProductName\"" = \""8:.*)"); 

Note the . after the 8:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top