0

I need to find anything inside a tr...

<tr class="class1">
    more tags here,
    multiple lines...
</tr>

How can I get anything that's between <tr class="class1"> and </tr>?

thanks!

開発者
  • 670
  • 1
  • 9
  • 11
  • Regular expressions are not suitable for parsing HTML as HTML is not a [regular language](http://en.wikipedia.org/wiki/Regular_language). Please look into DOM parsers instead. – Daniel Vandersluis Sep 27 '10 at 04:04
  • possible duplicate of [What regex will match text excluding what lies within HTML tags?](http://stackoverflow.com/questions/179779/what-regex-will-match-text-excluding-what-lies-within-html-tags) – Daniel Vandersluis Sep 27 '10 at 04:09

2 Answers2

1

You need to use a real HTML parser, regex isn't sufficient to perform this task.

That said, you can use a poor expression like this: /<tr.*?>(.*?)<\/tr>/ where group 1 will have what's (generally) between the <tr> tags, but no guarantees on correctness...things like nested tags will throw this off. You need to use a real HTML parser.

Community
  • 1
  • 1
Mark Elliot
  • 75,278
  • 22
  • 140
  • 160
  • I just need to get what's between the trs... doesn't matter if it's well formed or not, and what it is inside – 開発者 Sep 27 '10 at 04:06
  • 2
    It does matter if it's well-formed or not, because if you were to create a regex, you'd have to take that into account. Also there could be infinite nesting of ``s, and regexes are not recursive. HTML parsers are the way to go. – Daniel Vandersluis Sep 27 '10 at 04:08
  • thanks, but nothing is matched... I'm using .net, any special settings should be set? – 開発者 Sep 27 '10 at 04:14
  • @開発者: see the corrected expression the `/` in `` had to be escaped. as far as I know there are no special settings required. You may not need the preceding `/` or trailing `/`. – Mark Elliot Sep 27 '10 at 04:16
  • You do need to drop the `/` for a .NET regex (`@"(.*?)"`) and to set `RegexOptions.Singleline` to allow the dot to match newlines. Other than that, what @Daniel Vandersluis said... – Tim Pietzcker Sep 27 '10 at 06:38
0

2 possibilites:

Client side: Jquery - http://docs.jquery.com/How_jQuery_Works

Server side: Linq To Xml - http://msdn.microsoft.com/en-us/library/bb387061.aspx

Examples can be added if needed. I however think that the articles I posted will be quite useful in getting you started.

Tomasi
  • 2,517
  • 6
  • 31
  • 43