Re:Can regular expressions parse HTML or not?
by johndcook on Thursday February 21, 2013 @10:22AM
(#5257868)
Goto statements are a good example. It's easier to recite "goto is harmful" than to say "In most situations, other control structures are more expressive and easier to maintain than goto statements. However, there may be rare occasions, particularly in low-level system programming, where goto statements could be preferable."
"Don't parse HTML with regex" is good general advice, but no more an absolute than avoiding goto statements.
Re:Can regular expressions parse HTML or not?
by Dylan16807 on Thursday February 21, 2013 @11:43AM
(#5258416)
>rare occasions, particularly in low-level system programming, where goto statements could be preferable
Or something as simple as "break 2;"
Re:Can regular expressions parse HTML or not?
by simonster on Thursday February 21, 2013 @10:22AM
(#5257867)
In my experience, an XPath (or CSS selector) to get relevant information is both easier to construct and easier to understand than a regular expression, and the resulting code is more likely to continue to work as the website changes. I don't see why you would subject yourself to using regular expressions unless your environment doesn't have an HTML parser.
Re:Can regular expressions parse HTML or not?
by fuzzix on Thursday February 21, 2013 @11:23AM
(#5258295)
In this particular instance, for reasons of a self-imposed contraint to favour ease of deployment and use by the target audience, I have no parser.
Using xpath (or just about anything else) would be smashing.
The likelihood of the markup changing to the point where the code breaks is unlikely, the target pages are open source mirrors exposed over HTTP. It even winds up being nicer code than the FTP handler.
A knee-jerk reaction to 'HTML' and 'regular expression' being used in the same sentence is like someone seeing 'goto' without understanding the context and shouting "goto considered harmful!"
I recently use some combination of perl's split() and regexes to trivially pull all links from a piece of markup. I had to suppress the "Don't parse HTML with regular expressions!" voices echoing in my head the whole time I was writing it. I'm OK with the code now, of course.