4 ms·
Who said regular expressions are bad practice? Regexes are great but they can get abused easily. Looking more carefully at the SO question, I am inclined to as
by crispyambulance 5y ago
Who said regular expressions are bad practice? Regexes are great but they can get abused easily.
Looking more carefully at the SO question, I am inclined to ask "why?" at least a couple of times because I suspect the answer to the deeper problem the SO OP needs to solve can be worked out with a DOM parser. If not, then definitely a SAX parser could solve that specific problem and it would be more robust than handcrafting a fussy regex.
- goto11 5y agoAs far as I can tell, a SAX parser does not expose the distinction between an opening tag and a self-closing tag. The tag `<foo />` would just emit a startElement event followed by an endElement, exactly the same as `<foo></foo>`. Which means it can't solve the specific problem the OP describes.
- crispyambulance 5y agoI would rather use a robust battle-tested parser and handle that edge case separately than to create regex for all of xhtml, copied off an opinionated blog, as a "hail-mary".
- goto11 5y agoBut the question is specifically about that edge case! So how to handle it, if the "robust battle-tested parser" is of no help?