Why are RegexOptions compiled into RegexOptions.None in MSIL?

This code

Regex regex = new Regex("blah", RegexOptions.Singleline & RegexOptions.IgnoreCase); 

after compiling in ILSpy it looks like this:

  Regex regex = new Regex("blah", RegexOptions.None); 

Why is this happening, and could it be the reason for the regular expression not matching in .Net 3.5? At 4.5 it works.

+6
source share
1 answer
 RegexOptions.Singleline & RegexOptions.IgnoreCase 

is bitwise AND and resolves to 0 (i.e. RegexOptions.None ).

The RegexOptions variable is as follows:

 [Flags] public enum RegexOptions { None = 0, IgnoreCase = 1, Multiline = 2, ExplicitCapture = 4, Compiled = 8, Singleline = 16, IgnorePatternWhitespace = 32, RightToLeft = 64, ECMAScript = 256, CultureInvariant = 512, } 

So in binary form:

 RegexOptions.SingleLine == 10000 RegexOptions.IngoreCase == 00001 

When using bitwise And we get:

  10000 AND 00001 ----- 00000 

Replace

 RegexOptions.Singleline | RegexOptions.IgnoreCase 

which gives:

  10000 OR 00001 ----- 10001 

What ILSpy decompiles into:

 Regex regex = new Regex("blah", RegexOptions.Singleline | RegexOptions.IgnoreCase); 

But I do not know what "works" in .Net 4.5. I just compiled your code, and ILSpy also outputs:

 Regex regex = new Regex("blah", RegexOptions.None); 

as expected.

+14
source

All Articles