views:

743

answers:

5

Greetings!

I have some HTML that may or may not be valid. If the HTML is invalid, a best attempt can be made, and any errors that arise are acceptable (ie, grouping too much because some tag isn't closed correctly).

In this HTML are a variety of elements, some of which may have a class (call it "findme"). These elements are of varying type; some img, some object, some a, etc.

I need a regex that will pull out all the elements, and the content they contain if they contain content.

For example:

<div>
<span><img class="findme" src="something" /></span>
<object class="findme" classid="clsid:F08DF954-8592-11D1-B16A-00C0F0283628" id="Slider1" width="100" height="50">
  <param name="BorderStyle" value="1" />
  <param name="MousePointer" value="0" />
  <param name="Enabled" value="1" />
  <param name="Min" value="0" />
  <param name="Max" value="10" />
</object>
</div>

Running the regex on that chunk of HTML should return 2 elements:

<img class="findme" src="something" />

and

<object class="findme" classid="clsid:F08DF954-8592-11D1-B16A-00C0F0283628" id="Slider1" width="100" height="50">
      <param name="BorderStyle" value="1" />
      <param name="MousePointer" value="0" />
      <param name="Enabled" value="1" />
      <param name="Min" value="0" />
      <param name="Max" value="10" />
    </object>

Any of you regex gurus out there have an idea on this one?

Edit: The language is c#.

+2  A: 

Regex is an extremely poor tool for this job. Use a parser. And before you do, run it through HTML Tidy to fix the invalid HTML. Whatever language you're using (you don't say) will have one or more HTML parsers available.

cletus
A: 

Rather than try to solve this directly with raw regex operations,
you should consider using some HTML Parser module in any of the languages you are familiar.
Listing a few references here,

nik
A: 

You don't mention which language you're using, but you should probably be loading this into an XmlDocument and searching through the DOM properly. A regex will pattern-match but you could get all sorts of false positives.

A: 

Trying to solve this kind of problem with regular expressions is a recipe for desaster. If you're working on the server, parse the snippet with a fault-tolerant html parser (ie. hpricot for ruby) and validate against the dom. Same can be done on the client with el.innerHTML = "..."

flitzwald
+4  A: 

While regular expressions can be good for a large variety of tasks, I find it usually falls short when parsing HTML DOM. The problem with HTML is that the structure of your document is so variable that it is hard to accurately (and by accurately I mean 100% success rate with no false positive) extract a tag.

What I recommend you do is use a DOM parser such as SimpleHTML and use it as such:

require_once('SimpleHTML.class.php')

$html_dom = str_get_dom($html);
$tags = $html_dom->find('img.findme'); // Get all img elements of class findme

Some may think this is overkill, but in the end, it will be easier to maintain and also allows for more extensibility. For example, using the DOM parser, I can also get the alt attribute.

A regular expression could be devised to achieve the same goal but would be limited in such way that it would force the alt attribute to be after the src or the opposite, and to overcome this limitation would add more complexity to the regular expression.

Also, consider the following. To properly match an <img> tag using regular expressions and to get only the class attribute (captured in group 2), you need the following regular expression:

<\s*img\s+[^>]*?\s*class\s*=\s*(["'])((\\?+.)*?)\1[^>]*?>

And then again, the above can fail if:

  • The attribute or tag name is in capital and the i modifier is not used.
  • Quotes are not used around the class attribute.
  • Another attribute then class uses the > character somewhere in their value.
  • Some other reason I have not foreseen.

So again, simply don't use regular expressions to parse a dom document.

Andrew Moore
Fair enough. Previously the requirements were much simpler, so a regex worked fine, but I think you're right, this is out of the league of regexes.
jvenema