-3

Possible Duplicate:
Best methods to parse HTML with PHP

I'm trying to parse a webpage using RegEx, and I'm having some trouble making it work in a reliable manner.

Say I wanted to parse the code that creates a div element, and I want to extract everything between <div> and </div>. Now, this code could just be <div></div>, but it could also very well be something like:

<div class="thisIsMyDivClass"><p>This text is inside the div</p></div>

How can I make sure that no matter how many characters that are in between the greater-than/less-than signs of the initial div tag and the corresponding last div tag, I'll always only get the content in between them? If I specify that the number of characters following < can be anything from one to ten thousand, I will always be extracting the > after ten thousand characters, and thus (most likely, unless there is a lot of code or text in between) retrieve a bunch of code in between that I don't need.

This is my code so far (not reliable for the aforementioned reason):

/<.{1,10000}>/

Community
  • 1
  • 1
wstr
  • 910
  • 2
  • 13
  • 23
  • 2
    The best thing would be to use a proper HTML parser. What language are you using? – Pekka Jul 14 '11 at 00:48
  • I'm using PHP. Would love some good tips on how to do this in the most efficient way. – wstr Jul 14 '11 at 00:50
  • 5
    Check out [Best methods to parse HTML with PHP](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html-with-php) – Pekka Jul 14 '11 at 00:52
  • 2
    You could use something like `<[^>]*>`, but like everyone else is saying, you need a proper parser. – Daniel Haley Jul 14 '11 at 00:54
  • 2
    Just for context, this is one of the most frequently asked questions on StackOverflow, with [this being the most famous version](http://stackoverflow.com/q/1732348/143295). For the reasons you've stated and others (attributes containing ">" etc.), it is not possible to be 100% reliable with regex. You can use rough methods like @DevNull's in some cases, but just be aware of the limitations. – Nicole Jul 14 '11 at 01:50

3 Answers3

4

Regular expressions describe so called regular languages - or Type 3 in the Chomsky hierarchy. On the other hand HTML is a context free language which is Type 2 in the Chomsky hierarchy. So: There is no way to reliably parse HTML with regular expressions in general. Use a HTML parser instead. For PHP you can find some suggestions in this question: How do you parse and process HTML/XML in PHP?

Community
  • 1
  • 1
ChrisWue
  • 18,612
  • 4
  • 58
  • 83
1

You will need a Lexical analyser and grammar checker to parse html correctly. RegEx main focus was for searching strings for patterns.

Chad
  • 2,938
  • 3
  • 27
  • 38
1

I would suggest using something like DOM. I am doing a large scale site with and using DOM like crazy on it. It works, works good, and with a little work can be extremely powerful.

http://php.net/manual/en/book.dom.php

joe Hughes
  • 87
  • 1
  • 1
  • 6