I have the following two patterns and need to find a match if either or is found in a string and then move onto the next match.
for one pattern i’ve been using
MatchCollection m0 = Regex.Matches(file, @"<ul class=""Information"">[\s\n\t.]+<img src=""http://test/images/I/test.gif"">",
RegexOptions.Singleline);
then looping through the matches and extracting the details I need
The second pattern I need is
MatchCollection m1 = Regex.Matches(file, @"<ul class=""Information"">[\s\n\t.]+ <a href=""http://www.test.com/test/""\w+",
RegexOptions.Singleline);
Is it possible in regex to combine these into one search, using an OR for example?
Disclaimer
For the haters out there, this isn’t neccesarily a Parsing HTML with Regex Question. The root of his question is he’s looking for the Disjunction functionality in a regex.
Solution
While @dtb may be right about regex with html, for simple cases (small inputs & simple patterns) it’s not neccesarily a bad idea if you know when to use it.
This is a simple solution in regex.
What’s been done here @Paul is we’ve taken the two trailing components of your matches and put them into a non-capturing group:
(?: ). This makes sure it won’t mess with your capture groups, but you’re welcome to capture it if you need to.From here we use the OR operator
|to split the group into capturing either one or the other.This is the resulting pattern match for the 2nd part.
Note: If you are doing this more than once in a while (i.e. a loop) you should compile your regex, it’ll execute faster.