简体   繁体   中英

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

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.

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM