I have the following very simple regex, which matches HTML tags in a string. I have the case insensitive option set, so that capitalisation of the tags doesn't matter. However, when the 'compiled' option is set, then the 'IgnoreCase' option seems to be ignored.
Sample code:
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 anybody got an idea what is going on here?
In Java, by default, the regular expression (regex) matching is case sensitive. To enable the regex case insensitive matching, add (?) prefix or enable the case insensitive flag directly in the Pattern. compile() .
Discussion. Currently, REGEXP is not case sensitive, either.
Regex search is case insensitive · Discussion #10708 · community/community · GitHub.
You are using a bitwise AND for your flags, you should be using a bitwise OR.
This bit:
RegexOptions.IgnoreCase & RegexOptions.Compiled
Should be:
RegexOptions.IgnoreCase | RegexOptions.Compiled
Here is a good article on how flags and enumerations work in respect to C#.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With