.NET Regex Expression to find and replace any ProductName value in .vdproj file in C #

I am trying to write a NAnt extension task that can update various parameters in the Setup.vdproj file created in Visual Studio 2003, and would appreciate help in the following.

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

I am looking for a RegEx expression to change "ProductName" to any other value, without relying on anything else, except that the search string starts with "ProductName" = "8": and then has 1 or more characters and ends with an icon. I tried the following to no avail:

Before executing the following code snippet, the .vdproj ProductName file reads:

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

... and a code snippet 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 snippet, the .vdproj ProductName file now reads:

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

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

Any ideas and help would be greatly appreciated.

+3
source share
1 answer

You're almost there - just missed a single one.
Change your regular expression this way and happiness should follow ...

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

Pay attention to. after 8:

+2
source

Source: https://habr.com/ru/post/1724245/


All Articles