Skip to content Skip to sidebar Skip to footer

Regex Options Matching Multi-line As Well As Ignoring The Case

I have some piece of ill-formed html, sometimes the ' is missing. Also, it sometimes shows capital cases while other times lower cases:
Copy

Notice the addition of several \s* entries.

The second problem is that you're not concatenating the options properly.

Your code:

Match m = Regex.Match(html, pattern, RegexOptions.IgnoreCase & RegexOptions.Singleline);

Since these are bit flags, Bitwise-And (& operator) is a wrong flag. What you want is Bitwise-Or (| operator).

Bitwise-And means "if the bit is set in both of these, leave it set; otherwise, unset it. You need Bitwise-Or, which means "if the bit is set in either of these, set it; otherwise, unset it."

Solution 2:

You need to OR them together in this case.

const string pattern= @"<div class=""?main""?><div class=""?subsection1""?><h2><div class=""?subwithoutquote""?>(.+?)</div>";
Match m = Regex.Match(html, pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline)

Edit: Change your RegEx to the following ...

const string pattern = @"<divclass="?main"?>\s*<divclass="?subsection1"?>\*+<h2>\s*<divclass="?subwithoutquote"?>(.+?)</div>

Post a Comment for "Regex Options Matching Multi-line As Well As Ignoring The Case"