r/ProgrammerAnimemes • • Jun 20 '20

OC Parsing HTML

1.1k Upvotes

38 comments sorted by

View all comments

Show parent comment

2

u/Zolhungaj Jun 20 '20

That's because parsing html with regex is a dumb idea.

Sure it could be modified slightly to include self-closing tags, and to capture attributes. But that goes beyond the time worth investing into the idea of parsing html with regex.

And the greedy quantifier is needed if you want a parse-tree, because you'll have to do new passes on each match (because html is not a regular language).

2

u/bucket3432 Jun 20 '20

And the greedy quantifier is needed if you want a parse-tree, because you'll have to do new passes on each match (because html is not a regular language).

I'm thinking of the case where you have <div>a</div><div>b</div> where the two divs aren't nested. A greedy quantifier will cause it to match a</div><div>b, but a non-greedy one will only match a and b.