66 points · by Anon84 · 17y ago · 33 comments
There are plenty of potential areas involving HTML tags where you can manipulate the document with regexes, so long as you're not actually trying to make a proper parsing job of it. But everyone was so keen to show that they know you don't use regexes to parse HTML, they blew the actual question away. The asker did try to point out that he didn't want to parse the HTML, but still the points kept on rolling up for an answer that ... told him not to try and parse the HTML.
I think, to sum up this whole drama, we have one group saying "you should use a parser" and the other group saying "but sometimes regexes aren't totally horrible, and this is just a quick job. I don't want to take the effort to use the proper tool."
The latter group sees themselves as being pretty reasonable. And perhaps they are. But the thing that exasperates the former group is that it is generally not quicker or easier to use regexps -- even for one-off jobs --- than using a nice parser like BS or lxml.
Those tools give you a object-view of the document which, as JGC demonstrated, allows you to express what you want to do concisely and easily, even (especially) when you're picking a few tags out of the document.
The reason why regex hacking on X/HTML documents remains popular isn't because it's 'easy' but because it doesn't require people knowing how to use the proper tools, nor does it require people to make the conceptual leap from the document as a long string of unstructured text to the document being a tree-structure of objects.
It's a classic "The Wrong Way" choice made by people who don't have a mechanical or conceptual understanding of the right way.
It's like this. Imagine you come upon someone banging in screws with a hammer. And laying next to them is a nice power drill with a Phillips bit. You say to them "why aren't you using the drill for that. It'd do a better job and be a hell of a lot easier too".
And the guy responds "This is a quick job. These screws don't need to hold very much weight, or for very long. If this was a serious piece of construction I would totally use the drill, but for my needs this is just easier."
Now, you might look at him with some incredulity. No way is his way "easier." It's much harder, on top of doing a worse job. The real reason why he's not using the drill is, quite obviously, he doesn't know how!
So you say to him "seriously, try the drill" and he keeps on insisting that his approach is "totally sufficient" (which it may well be, that's not the point) and that using the drill would be "too much work."
And then that's how we got here in this discussion.
> Imagine you come upon someone banging in screws ...
> You say to them "why aren't you using the drill ...
> And the guy responds "This is a quick job. ...
> The real reason why he's not using the drill is,
> quite obviously, he doesn't know how!
Your point is well taken, and your anecdote/metaphor is evocative. However, for the sake of completeness, I offer some information.In some professional theatres the ones responsible for constructing the scenery sometimes, in some cases often, hammer in screws. It's faster and sufficiently reliable, and when it comes time to dismantle they simple unscrew with a power tool.
It's faster because using a power screwdriver often requires drilling a pilot hole first, whereas hammering it in, doesn't.
These are seasoned professionals. They know the tools, they've experimented with different techniques, they've evolved the best way of working for the circumstances.
Don't assume you're right, just because you think ...
> ... quite obviously, he doesn't know how!
Sometimes the other guy who does this all the time actually does know better than you.Everybody is assuming he wants to parse this document, which isn't proven and is mostly unfounded. What if it's the text of a book, including all sorts of mentalism that will blow up any parser? Nowhere does it say that he's got valid (or invalid) HTML/XML here.
It's actually as if he said "hey, can I borrow a screwdriver?" and everyone went "He wants a screwdriver? He must be building a box, and that means nails! Give him a hammer! A hammer! You can't put in nails with a screwdriver, Zalgo wouldn't do that!"
So now he's off trying to put his screws in with a hammer, because in the race for points people wouldn't first ask him to clarify the problem domain and work out what he's actually trying to achieve.
I think HTML parsers have their place, but I don't understand why people think regular expressions are not a valid tool for getting data out of HTML. HTML parsers are only an appropriate solution if the HTML is valid, well structured and mostly semantic.
If you are dealing with invalid, code generated via Frontpage 2000 by some bureaucrat with little HTML knowledge, regular expressions will almost always be a better option.
You can't parse HTML with regex.
You can parse text blobs with regex.
If your input looks like HTML but is really a text blob, you can parse it with regex.
I loooove it when a site justifies my use of AdBlock.
You could write a scanner that doesn't use regexes. It could just read in characters one at a time and hard-code the logic for matching tokens.
On the other hand, I wouldn't be surprised if many of the standard html parsing libraries use regex libraries internally to match tokens.
If you used a scanner generator like lex, you would specify your scanner using regexes and lex would generate code that expands those regexes into an automata. I guess that also counts as using regexes, only they're compiled down at compile-time instead of at runtime.
Anyone who has had to data mine HTML on a regular basis knows regular expressions are the only way to go because most web pages are clusterfucks of invalid, font-tag laden, id-less, ancient piles of shit.
Any XML parser I threw at the sites I was mining ran away crying with it's pants soaked with urine. So climb down your ivory tower, because XML parsers may be the "correct" way to get data out of HTML, but they are near worthless for most situations.
Most people are ignorant of how bad most of the HTML on the web is, it is simply unparseable.
If that was the case the web would be unusable. Any decent web browser will parse almost any garbage you throw at it. It can get complicated because you find yourself guessing at what the author's intent might have been or moving things around to make it semi-sane, but it's not only possible; it's done every day.
And somehow using a regex to guess at all the possible combinations of "badness" is the correct way to go? You seem lost my friend. Everything you're saying is backwards to what someone who actually parses a lot of HTML "in the wild" would say on the topic. It sounds more like you're trying to cuss your way into being correct.