3 ms·
> known-quantity HTML where regex will work just fine (say, extract all href out of all a elements), Regex doesn’t work fine even on that. You still need to te
by pwdisswordfish8 5y ago
> known-quantity HTML where regex will work just fine (say, extract all href out of all a elements),
Regex doesn’t work fine even on that. You still need to tell apart actual tags from contents of attributes, comments, uninterpreted contents of <script>, and so on. A regex alone doesn’t give you the ability to do that. If you want to parse HTML, you need a parser. Simple as that.
- AnIdiotOnTheNet 5y agoOnly if you care about absolute accuracy in sufficiently complex pages. There are many use cases I can think of that really don't.
- TeMPOraL 5y agoParsing HTML with regexes is fine if you're not solving a general case. A regex "parser" will carry implicit assumptions about the document structure. Like, I know "<div.+>.+foo\s+([a-z]+)" will work, because I've crafted it for a specific site whose sources I've seen.
- ziml77 5y agoNot sure why you're downvoted. What you're describing is perfectly reasonable. Like you might be scraping a page with no user-generated content. Regex can make a lot of sense because if the regex stops matching, the page probably changed enough that a parser wouldn't stop it from breaking the scraping either.
- coldtea 5y ago>Regex doesn’t work fine even on that. Most of the time it works just fine for my purposes. And it's not like I don't know alternatives. And I've written a toy compiler in the past (C with classes subset), done heavy XSLT work, know how to write a parser (manually, with flex/yacc, with PEG), have parsed XML with SAX, DOM, XOM, have used xpath expressions, css and jquery selectors, and used libs like BeautifulSoup. Most of the time a quick regex will do just fine for that purpose. See, I don't intend to send the href's to the ISS or to some medical equipment that will blow up if, god forbid, I accidentally found an A tag in a comment or inside a script or attribute. And I'll probably run another regex or simple editor or awk filter to get rid of some false positives and I'll be fine. >If you want to parse HTML, you need a parser. That's a tautology, so it's always true. If on the other hand you just need to extract some information from an HTML file, and you can just open the bloddy file and see its structure and it is perfectly servicable with a regex, you might not need a parser at all. Simple as that.
- jameshart 5y agoIf you're in a situation where you know enough about both HTML and regex to know that you can definitely solve your HTML parsing problem safely using a regex... What possible question could you have for an online forum?