Regular expressions in C#

Learnt something new today from Merak on the UK Microsoft web developer’s list.

One of my early problems when writing regular expressions in C# was that I didn’t realise that is actually an escape character when used in a string in C#.

So when I wanted to write an expression like this:


(which means “is digit”)

I wrote:

Regex isNumber = new Regex("d");

When it should have been:

Regex isNumber = new Regex("d");

The extra slashes make the expression even harder to read, so what you can do instead is:

Regex isNumber = new Regex(@"d");

Posted on 19 Oct 04 by Helen Emerson (last updated on 19 Oct 04).
Filed under ASP.NET