6 ms·
>Best practices are a very wide category, don't parse HTML with regular expressions is the kind of thing you really should just say don't do that. I call BS.
by coldtea 9y ago
>Best practices are a very wide category, don't parse HTML with regular expressions is the kind of thing you really should just say don't do that.
I call BS.
"Don't have a continuously running service parse arbitrary HTML with regular expressions" would be a bad thing.
Parsing specific, given, HTML files, with known structure to the dev, with regular expressions (e.g. as part of a one-off scrapping script) is totally, absolutely, fine.
If my HTML file is like:
<html>
<body>
<a href="xxxxx">foo</a><br>
<a href="xxxxx">foo</a><br>
<a href="xxxxx">foo</a><br>
<a href="xxxxx">foo</a><br>
<a href="xxxxx">foo</a><br>
<a href="xxxxx">foo</a><br>
<a href="xxxxx">foo</a><br>
</body>
</html>
I can parse it just fine with regex.
And even parsing is the wrong word: I can extract the information I want is a better term. I won't be parsing anything in the AST sense.
- deleted 9y ago[deleted]
- Retric 9y agoThat is a trap and why people give that advice so freely. Sure, today you just want to parse these files just the once an never again, ever, really I mean it. Can you determine if it's <html><body>Yes</body></html> Vs <html><body>No</body></html> well sure and even more complex operations also 'work'. However, if you control what's going on then don't use regular expressions it's wasteful overhead. If you don't control it then you can't tell if it will stay like that, which is the trap. Things that don't change the meaning like swapping the order Attributes which does not change the meaning will often force an update to your regular expression. PS: It's also why these are called best practices not physical laws.
- coldtea 9y ago>If you control what's going on then don't use regular expressions it's wasteful overhead. Not really, if you control what's going on it's a very fast option -- use awk, sed, or your scripting language's regex lib, and get the values you need. End of story. Nobody cares if you can micro-optimize it with lower level string parsing that doesn't use a regex engine -- and the regex engine might end up being faster that that anyway. >If you don't control it then you can't tell if it will stay like that, which is the trap. No, but I already covered that in my original comment. One can do it to extract values for a specific, known html file, I said. Besides, (a) a lot of changes can still be caught by regex -- e.g. the order of attributes don't matter if the regex checks for that specific attribute alone (e.g. a href in an a tag). (b) other changes will also fuck any non-regex, parser-based lib. E.g. if the nesting changes or an element is moved to another section. It's not like you don't have to update scripts written with e.g. BeautifulSoup or some HTML parser when the document changes. >It's also why these are called best practices not physical laws. That's up to those who advocate them to understand: and don't hand them out as if they were physical laws "NEVER PARSE HTML WITH REGEX".
- Retric 9y agoI don't mean CPU overhead. I mean you are creating a brittle process that's hard to verify it actually worked, unless you have access to the data some other way in which case it's mostly pointless. Regular expressions are not going to tell you if one of the files just happens to have bad data or any number of things that's going to cause problems.
- phil21 9y agoAs someone who has created more hacks like you describe than I care to admit, you actually make the case of precisely why you don't parse HTML with regex. Because that code works fine for 5 years until it blows up after something upstream changes. From a RoI calculation this might be perfectly fine if you're still around and remember how to fix it - it's a 5 minute fix after all! But if you've left, or if that system became old and crufty and fragile you may have just burned 3 days tracking it down. If someone is asking this question, it is entirely appropriate to tell them "don't ever do that" - they obviously lack the experience and big picture thinking to know when it is appropriate to deviate due to business reasons. If you leave it at that I agree you're being a dick - whenever shooting someone's idea down you had damn well better have some appropriate alternative options.
- dave84 9y agoSerious question... why would any other approach be exempt from the problems you stated?
- phil21 9y agoNothing is exempt, some things are far less fragile than others though.
- ivanhoe 9y agoBut are also significantly slower and consume hundreds of times more memory, and will still break with the first little change in the DOM tree on the path that you rely on - which smart regexps can sometimes handle. For instance if you're looking just for a title and a price of a single product (common requirement for spiders), you can extract that with regexp without caring about the html page at all, just pick the <h1> and something that looks like a dollar value. I had spiders like that kept working after a full redesign of the site where even the platform was moved from WP to Magento. No DOM parser could handle that, plus they would be too slow in the first place. You need to pick the right tools for the job, everything else is BS...
- jerf 9y ago
- titanix2 9y agoIn the case of processing HTML with regex there is a strong theoretical argument against it: a regular expression is a type 3 language while HTML is described by a type 2 grammar. Type 3 being a subset of type 2, regular expressions are not expressives enough to handle the grammar of HTML. In most cases of best practices however, such a mathematical argument doesn't exist. So, it's more of a battle of (not so) informed technical opinions and experiences, where drawing a clear line is impossible. Reading list: https://en.wikipedia.org/wiki/Chomsky_hierarchy https://en.wikipedia.org/wiki/Chomsky_hierarchy https://stackoverflow.com/a/14207715 https://stackoverflow.com/a/14207715