I'm writing a quick utility program in C# to modify all of my Visual Studio C# item templates to add extra using ;
statements. I've written a simple regular-expression to (hackishly) extract the set of current namespaces imports in each file so I can add my desired new imports without duplication.
I'm using Regex101.com to test my regular-expressions before inserting them into my C# program, but when I tested them in my C# program they didn't work. This confuses me because the regex itself is trivial, it uses regex conventions common to both .NET and ECMAScript regular-expressions and it also fails even with the RegexOptions.ECMAScript
compatibility option.
const String input = @"using System;
using System.Foo;
using Foo.Bar;
namespace Foo
{
using Baz;
class Qux
{
}
}";
Regex regex = new Regex( @"^using ([\w\.]+)\;$", RegexOptions.Multiline | RegexOptions.ECMAScript );
Match match = regex.Match( input );
Assert.IsTrue( match.Success ); // `match.Success` is false when I run this code
I don't understand why, because the behaviour of \w
, ^
, $
and the multiline options is ostensibly the same in both platforms.
Here is a screenshot showing it succeeding in Regex101.com:
Here is a screenshot showing it failing in .NET: