Reputation: 1945
Basically, I've got a string like this:
<table><tr class="even"><td></td><td></td></tr><tr class="odd"><td></td><td></td></tr></table>
I want to get each tablerow using Regex.Matches (c#)
What would my regex pattern be?
I've tried the following but I think it's probably pretty wrong:
"<tr"(.*)"</tr>"
[<][t][r](.*)[<][/][t][r][>]"
But even
[t][r].*[t][r]
Gives 0 matches.
Any idea?
Thanks a lot!
Upvotes: 0
Views: 2224
Reputation: 5688
var str = "<table><tr class=\"even\"><td></td><td></td></tr><tr class=\"odd\"><td></td><td></td></tr></table>";
string[] trs = Regex.Matches(str, @"<tr[^>]*>(?<content>.*)</tr>", RegexOptions.Multiline)
.Cast<Match>()
.Select(t => t.Groups["content"].Value)
.ToArray();
the "trs" array is your result, containing <td></td><td></td>
two times.
edit: This regex is not resilient to missing a </tr>
(wich would display fine in any browser). This could be solved using a negative lookahead
Upvotes: 0
Reputation: 3671
Try this:
var s = @"<table><tr class=""even""><td></td><td></td></tr><tr class=""odd""><td></td><td></td></tr></table>";
Regex.Matches(s, @"(<tr .*?</tr>)", RegexOptions.Singleline)
Upvotes: 0
Reputation: 6175
\<tr[\s\S]*?\/tr\>
.?* : All Characters, stop at first match.
\s\S : All Characters, even new lines.
I use RAD Software Regular Expression Designer to create and test REGEX expressions. It is a great tool!
Upvotes: 2