3 ms·
A clickbait, and wrong, title, for an otherwise interesting article. I could do without the cutesy tone and anime, though. You shouldn't parse HTML with regex.
by imiric 1y ago
A clickbait, and wrong, title, for an otherwise interesting article. I could do without the cutesy tone and anime, though.
You shouldn't parse HTML with regex. XML and strict XHTML are a different matter, since their structure is more strictly defined. The article even mentions this.
The issue is not that you can't do this. Of course you can. The issue is that any attempt will lead to a false sense of confidence, and an unmaintainable mess. The parsing might work for the specific documents you're testing with, but will inevitably fail when parsing other documents. I.e. a generalized HTML parser with regex alone is a fool's errand. Parsing a subset of HTML from documents you control using regex is certainly possible, and could work in a pinch, as the article proves.
Sidenote: it's a damn shame that XHTML didn't gain traction. Browsers being permissive about parsing broken HTML has caused so much confusion and unexpected behaviour over the years. The web would've been a much better place if it used strict markup. TBL was right, and browser vendors should have listened. It would've made their work much easier anyway, as I can only imagine the ungodly amount of quirks and edge cases a modern HTML parser must support.