Reliably Parsing HTML Elements Using RegEx
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
Solution 1:
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?
Solution 2:
You will need a Lexical analyser and grammar checker to parse html correctly. RegEx main focus was for searching strings for patterns.
Solution 3:
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.
Post a Comment for "Reliably Parsing HTML Elements Using RegEx"