3 ms·
This response to someone on StackOverflow asking how to parse HTML with regex: https://stackoverflow.com/a/1732454/145684 https://stackoverflow.com/a/1732454/14
by steven_noble 9y ago
This response to someone on StackOverflow asking how to parse HTML with regex: https://stackoverflow.com/a/1732454/145684 https://stackoverflow.com/a/1732454/145684
- shubhamjain 9y agoI used to love this answer but after I realised that it's same irrational peddling of best practices as "don't-use-goto". There can be many cases where parsing HTML with Regex is abuse, but equally others where it's the best available solution. I wasted countless hours in working with lame HTML libraries, their limited options for selectors, their frustrating failures with invalid HTML, all because of this advice. Looking back, I think Regex would have worked just fine.
- arvinjoar 9y agoYes, a thousand times this. First of all, regex in the wild (e.g. Perl regex) is much more powerful than the CS version (that can only handle regular languages). This point is often conceded though from the "don't use regex to parse HTML" side, but some don't realize this. Another thing is that you don't really need to handle HTML at all, only a small subsection that might be totally fine with a regex, even a simple one, for a lot of cases. The true enemy is parsing something that might change over time, and that's totally unrelated to the regex issue.
- tmaly 9y agoI have done plenty of regex parsing of xml with Perl. It has been very useful. Over time I have also used things like the index function to eck out some additional performance. Recently I replaced this with a xml tokenizer I wrote in Go that can deal with invalid or corrupt xml. On top of this I have used a state machine to make it possible to handle different situations.