How to perform regular expression based replacements on files with MSBuild

Posted by Daniel Cazzulino on ASP.net Weblogs See other posts from ASP.net Weblogs or by Daniel Cazzulino
Published on Wed, 05 Dec 2012 02:39:27 GMT Indexed on 2012/12/05 5:05 UTC
Read the original article Hit count: 516

Filed under:

And without a custom DLL with a task, too Smile.

The example at the bottom of the MSDN page on MSBuild Inline Tasks already provides pretty much all you need for that with a TokenReplace task that receives a file path, a token and a replacement and uses string.Replace with that. Similar in spirit but way more useful in its implementation is the RegexTransform in NuGet’s Build.tasks. It’s much better not only because it supports full regular expressions, but also because it receives items, which makes it very amenable to batching (applying the transforms to multiple items). You can read about how to use it for updating assemblies with a version number, for example.

I recently had a need to also supply RegexOptions to the task so I extended the metadata and a little bit of the inline task so that it can parse the optional flags. So when using the task, I can pass the flags as item metadata as follows:...

Read full article

© ASP.net Weblogs or respective owner

Related posts about .NET