Wednesday, October 17, 2007

C# .NET Regular Expressions gotcha

I sure do love me some regular expressions but haven't really had the need in any of my C# code until now.

A few things did catch me out though, in particular the constructor for the Regex class.

The constructor signature I'm interested in is this one: (string pattern, RegexOptions options). All well and good until you want to set multiple options!

Now I might be slightly naive but why doesn't the constructor use the params keyword allowing it to accept a variable number of RegexOptions? The constructor would then look something like (string pattern, params RegexOptions[] options). I think this follows the LOLA, unlike the current method of passing multiple regex options to the constructor.

Oh, guess how you are supposed to do it? Via a bitwise OR of the enum values ofcourse! Like my NUnit fun, at least I can now just get on with it.


 1 Regex matchFieldValue =
 2   new Regex(@"
 3     (?<Field>\w+) #capture 1 or more word characters to the named group 'Field'
 4     \s*=\s*       #followed by 0 or more spaces, an = sign and 0 or more spaces
 5     (?<Value> '   #match an apostrophe
 6         ([^'] |   #match 0 or more of any character that is not an ' OR
 7          ''.)*    #double apostrophe's followed by any character          
 8     ')            #match the closing ' and capture to named group 'Value'
 9             ",
10              RegexOptions.Compiled |
11              RegexOptions.IgnoreCase |
12              RegexOptions.IgnorePatternWhitespace);

No comments: