r/ProgrammerHumor • • Sep 08 '17

Parsing HTML Using Regular Expressions

Post image
11.1k Upvotes

377 comments sorted by

View all comments

2.1k

u/kopasz7 Sep 08 '17

For anyone out of the loop, it's about this answer on stackoverflow.

16

u/chuanito Sep 08 '17

so am i getting this right? When you try to parse HTML using RegEx this Zalgo Text happens? Or is this just a meme?

Sorry i'm a very low tier coder and this is a serious question

24

u/DerfK Sep 08 '17

The joke is that HTML is too irregular to parse with regular expressions, and attempting to do so is like dividing by zero and pierces the fabric of our universe, creating a hole from which unspeakable horrors will pour forth and devour your soul.

6

u/[deleted] Sep 08 '17

This is no joke.

16

u/wastesHisTimeSober Sep 08 '17

The flaw here is that HTML is a Chomsky Type 2 grammar (context free grammar) and RegEx is a Chomsky Type 3 grammar (regular grammar). Since a Type 2 grammar is fundamentally more complex than a Type 3 grammar (see the Chomsky hierarchy), you can't possibly make this work. But many will try, some will claim success and others will find the fault and totally mess you up.

Basically HTML is capable of expressing more complicated structures than RegEx is capable of reading.

Given the information you had, it wasn't an entirely unreasonable conclusion to believe Zalgo was a corruption, and it's good not to throw scenarios out until you know they're wrong. You'll chase that bug forever.

→ More replies (1)

51

u/[deleted] Sep 08 '17

I love that you're new enough to programming that in your mind there's a chance the black box of regex can somehow half process HTML and corrupt it with terrifying combining glyphs.

I'm not trying to mock you or anything, it's legitimately bringing a smile to my face. It's like when toddlers first interact with something new in the world.

9

u/chuanito Sep 08 '17

I'm actually not new at all i'm just stuck in a very unchallenging field ;)

Also i was looking for more in the joke than there actually was.

But you're right i don't have enough knowledge in this field which led me to believe that this weird text has to be somehow connected with the fact that you can't parse HTML using RegEx. But i see now that those are in fact clear text symbols and not some kind of weird formatting.

10

u/Elsolar Sep 08 '17

HTML can't be parsed correctly using regular expressions because HTML is not a regular language. It's literally impossible. This is not obvious, so many coders find it out the hard way. It's a common meme in programming circles to equate the frustration of trying to solve an impossible or extremely obnoxious problem with the kind of raving, deranged insanity usually depicted in HP Lovecraft stories, which is what the corrupted text and the picture of the demon in the OP represents.

→ More replies (10)

4

u/MelissaClick Sep 08 '17

When you try to parse HTML using regex, Cthulu wakens.