Live data from Hacker News

Parsing HTML with Regex

stackoverflow.com

1–10 of 34 posts

Re: Parsing HTML with Regex

#2
I had one class where we had to build a multi threaded search engine in java and parsing HTML with regex was a requirement. Regex was the downfall of about 50% of the class and the majority of the students who did well still had slight issues with their HTML parsing. Moral of the story is that regex is a poor solution for HTML. Not to mention, hours debugging regex is one of the least meaningful or rewarding experiences you can have as a programmer.

Re: Parsing HTML with Regex

#5
While I agree with the answer. What most people take from it is don't use Regex to scrape data from HTML. Which isn't exactly the point of it. Parsing HTML and scraping are two different things.

If you know the exact HTML you are working with, using regex to extract the data is in my opinion a superior way of doing it. Less lines and generally less complexity. (Such as taking the name and id of an amazon product from a single site is different from taking all the links out of any page given.)

Re: Parsing HTML with Regex

#7

I reckon that technically regex is a tool that can be used to parse HTML. It's just that you could only use it in a very trivial way that would be better suited to other tools.

Ruby regular expressions have a \g operator which lets you call a sub expression - so technically they could be used to parse HTML if you're a masochist.

Re: Parsing HTML with Regex

#8
post #6

You cannot parse HTML with regex. You can find and match strings, but you can't actually parse html with regex. Chuck Norris can parse HTML with regex.

"asking regexes to parse arbitrary HTML is like asking Paris Hilton to write an operating system"

This cracked me up :)

Re: Parsing HTML with Regex

#10
There are plenty of good, real html parsers, so there's no need to try regex.

Xpath is nice for scraping HTML, though it always turns out the stuff you need is in the middle of a bunch of other text.

Post reply on HN