The Regex 'Ignore Case' parameter does not work if the Compiled option is specified

I have the following very simple regular expression that matches the HTML tags in a string. I have a case insensitive parameter, so tag capitalization doesn't matter. However, when the "compiled" parameter is set, the "IgnoreCase" option seems to be ignored.

Code example:

string text = "<SPAN>blah</SPAN><span>blah</span>"; Regex expr1 = new Regex("</*span>", RegexOptions.IgnoreCase); Regex expr2 = new Regex("</*span>", RegexOptions.IgnoreCase & RegexOptions.Compiled); MatchCollection result1 = expr1 .Matches(text); //gives 4 matches- <SPAN>,</SPAN>,<span> & </span> MatchCollection result2 = expr2 .Matches(text); //only gives 2 matches- <span> & </span> 

Has anyone understood what is going on here?

+4
source share
1 answer

You use bitwise AND for your flags, you must use bitwise OR.

This bit:

 RegexOptions.IgnoreCase & RegexOptions.Compiled 

Must be:

 RegexOptions.IgnoreCase | RegexOptions.Compiled 

Here is a good article on how flags and enums work in relation to C #.

+15
source

Source: https://habr.com/ru/post/1411782/


All Articles