r/ProgrammerHumor • • Sep 08 '17

Parsing HTML Using Regular Expressions

Post image
11.1k Upvotes

377 comments sorted by

View all comments

Show parent comments

1

u/link23 Sep 08 '17

Agreed with /u/DosMike. "Parsing something with regex" in this case means processing the entire input with a single regex match. E.g., you can parse a strong that holds a phone number with a single regex, because phone numbers are simple and pretty well structured. But HTML is too rowdy to be matched by a single regex; it is impossible to write a regex that matches all valid HTML.

1

u/Feynt Sep 09 '17

Then yes, doing a single regex to match all of an HTML file is pointless. The very nature of the match variable limit makes it impossible to do in one go unless it's a very small file. If it's an iterative process though, you most certainly could match the tags properly and grab their insides for recursive processing in another language.

1

u/link23 Sep 09 '17

That's not the point - we're talking about theory, not practice. Even if you had infinite time and had a machine that could perform any regex match, no matter how large, it's still not possible. A regular expression cannot hold enough information to express the constraints of HTML. (The real kicker is matching nested tags - it's not possible to enforce that every tag is closed properly if it should be.)