5 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. Camelcase is
by Retric 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. Camelcase is the middle ground where it's a good idea but personal preference may show up yet people get just as dogmatic about it.
IMO, what trips people up is when wisdom says "don't use Oracle products" it's completely accurate, but not that helpful when you inherit a huge mess.
- mlevental 9y ago>don't parse HTML with a regular expressions Even if I agree with you (I don't know because I've never been in the situation where that was a thing I was considering and weighing against other options) what I'm arguing here is that if someone asks how to do that then you should either tell them along with the admonishment or not say anything at all. But don't go around being a dick by yelling "haha that's the dumbest thing ever and I'm not going to tell you because of best practices"
- Pamar 9y agoWhile I agree with you in general (i.e. educate people or just stay silent instead of pontificating/be condescending if not rude), what would you say about the canonical answer on StackOverflow, then? https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 https://stackoverflow.com/questions/1732348/regex-match-open...
- mlevental 9y agomeh. the tone isn't so bad but, while at first i thought otherwise, it doesn't really comprehensively explain why it's a bad idea (on first skim i thought most of the content of the answer was an explanation of the difference between a state machine and cfg or something like that). i think a good model for how to answer questions like this is really the same as you'd answer kids' questions as a parent (or maybe anyone's questions?): a flat out no is completely unsatisfactory and lazy. a no with a measured explanation is good.
- Pamar 9y agoAgain, agreed. But what when something is at the same time pretty much wrong but also something that everyone tries to do because superficially looks like a good idea? The reaction on StackOverflow was because they were getting dozens (hundreds) more or less identical copies of the same question. It was the same with "parse email via regexp" but I understand that more recent versions of the RFC make this doable.
- 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".
- cgdub 9y ago> you really should just say don't do that You should also say WHY not to do that. This lets whoever asked decide whether the reasoning applies to their own unique situation. Perhaps they are doing a one-off scrape of a single file with consistent structure like in the other comment. The asker can also apply the same reasoning to similar situations. They won't come back to ask if they should use regex to parse JSON tomorrow. And if you can't explain why something is "best practice", maybe it shouldn't be "best practice".
- nix0n 9y ago> you really should just say don't do that This is counterproductive unless you also say what to do instead.