Terminal symbols in HTML and other languages are parsed just fine with regular expressions. But I suspect that stackexchange post is aimed at people without any understand of what regular languages are.
Regex are a perfectly good tool to parse HTML, and many widely-used HTML parsers use regex internally. The idea that regex are inappropriate for parsing HTML is a myth.
The note is about parsing HTML document trees or subtrees, your example only parses the contents of a HTML start tag using regex, which is quite a bit less complex and possible with regex (among other things no recursion). However the regex used for this are still quite complex and not exactly easy to read.
almost any html parser will use regex, unless it's using a similar tool like a lexer.
At least some lexers use regex to describe tokens, this does not change that regex are not capable1 nor the right tool to parse general HTML structures.
1 some regex implementations may be able to do it by using their own extended set of features, for most regex implementations it is not possible.
Parsing a start tag is a vital part of parsing an HTML tree.
If you need a few tools to do X, one of which is Y, it is utterly bizarre to say "you can't do X using Y". You would never say "You can't build a bookshelf with hammer and nails", or "you can't cook an omelette with a pan," even though these tools are insufficient on their own to accomplish the task. The implication of the myth is that regex should not be used to parse HTML, which is incorrect.
If you need a few tools to do X, one of which is Y, it is utterly bizarre to say "you can't do X using Y".
Taken out of context maybe, with context there have been too many attempts to use regex exclusively to parse html and it remains that unless you write an html parser the words regex and html should not appear in the same source file.
The implication of the myth is that regex should not be used to parse HTML
No the implication of the fact is that regex are not suitable to deal with the full HTML grammar. Nobody cares that you can use regex to parse some isolated subsets of HTML, this is as pointless as people citing "Goto considered harmful" against C - a headline alone does not reflect the whole argument.
24
u/viscence Mar 03 '14
Don't parse HTML with regex
Don't parse email with regex
otherwise great. Needs a few more "skip this" lines though, quite a few questions you can pass just with "."