0

So i've searched all of the internet, and i can't find a working solution to my problem.

I have a HTML table

<tr>something</tr>
<tr class="even detailed">Something</tr>
<tr class="odd detailed">something</tr>
// and so on

Now i will all the content of the tr's with and even or odd class. but i only get 1 result. my C# code is:

pattern = @"<tr class=""(even|odd) detailed"">(.*)</tr>";
var matches = Regex.Matches(Contents, pattern, RegexOptions.Singleline);

I've tried lots of things, but it doesn't seem to work. With this i only get 1 result.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Marijn Kok
  • 121
  • 8
  • 2
    You should explore HTML 5 data attributes. jQuery selectors and data attributes are SO much cleaner than regex. – P.Brian.Mackey Nov 21 '13 at 20:12
  • 3
    [You cannot use regex to parse HTML](http://stackoverflow.com/questions/6751105/why-its-not-possible-to-use-regex-to-parse-html-xml-a-formal-explanation-in-la). Find an HTML parsing library for C#. – Tim S. Nov 21 '13 at 20:14
  • 5
    Only Chuck Norris has been able to successfully parse HTML using RegEx. – Icemanind Nov 21 '13 at 20:16
  • @icemanind And see, what it was worth: It blew out his brain and now he has to do [this](http://www.meetthenra.org/nra-member/chuck-norris) - so Regex on HTML is the only Nemesis for Chuck Norris. – Uwe Keim Nov 21 '13 at 20:19
  • 1
    Interesting. I didn't think Chuck Norris had a Kryptonite counterpart – Icemanind Nov 21 '13 at 20:21
  • You must only add a "?" after your "*" quantifier. – Casimir et Hippolyte Nov 21 '13 at 20:28

3 Answers3

1

Consider the following Regex...

(?<=\<tr.*?(even|odd).*?\>).*?(?=\<)

Good Luck!

gpmurthy
  • 2,397
  • 19
  • 21
0

Regular expressions, by default, follow the "leftmost-longest" rule, which is to say, that they are greedy and will find the leftmost, longest possible match. Your .* rule happilay (ecstacticly, even) consumes the all the </tr> elements save the last, along with all the <tr> elements save the first, leaving you with just one match.

Further, as @TimS noted, HTML is not a "regular" language in the sense of "regular expressions".

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
0

It is better solution to use a html parser. but if you really want to use a regex, this is a solution

regex:

<tr\s+class="(even|odd)\s+detailed">(.*?)</tr>

the code

string strRegex = @"<tr\s+class="(even|odd)\s+detailed">(.*?)</tr>";
RegexOptions myRegexOptions = RegexOptions.None;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = @"<tr>something</tr>" + "\n" + @"<tr class=""even detailed"">Something</tr>" + "\n" + @"<tr class=""odd detailed"">something</tr>" + "\n" + @"// and so on";

foreach (Match myMatch in myRegex.Matches(strTargetString))
{
  if (myMatch.Success)
  {
    // Add your code here
  }
}
lordkain
  • 3,061
  • 1
  • 13
  • 18