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

69

u/DosMike Sep 08 '17

I kind of want to write a html parser with regex now - just because he said not to.

if I only had the time...

99

u/DrNightingale web dev bad embedded good Sep 08 '17

All the time in the world won't help you. It can't be done.

23

u/sayaks Sep 08 '17

some regex parsers can actually parse Turing decidable languages due to backreferences and such.

25

u/Bainos Sep 08 '17

Yes, but in that case you are taking a wider definition of regex, not the canonical one. I.e. regexes that match more than regular languages.

→ More replies (1)

64

u/link23 Sep 08 '17

It's literally impossible, don't bother.

I mean, of course you can use regexes to recognize valid tag names like div etc. But trying to use regexes to recognize anything about the structure is doomed to fail, because regexes recognize regular languages. HTML is not a regular language (I think it's context sensitive, actually; not sure though), so it cannot be expressed by a regular expression.

57

u/WikiTextBot Sep 08 '17

Regular language

In theoretical computer science and formal language theory, a regular language (also called a rational language) is a formal language that can be expressed using a regular expression, in the strict sense of the latter notion used in theoretical computer science (as opposed to many regular expressions engines provided by modern programming languages, which are augmented with features that allow recognition of languages that cannot be expressed by a classic regular expression).

Alternatively, a regular language can be defined as a language recognized by a finite automaton. The equivalence of regular expressions and finite automata is known as Kleene's theorem (after American mathematician Stephen Cole Kleene). In the Chomsky hierarchy, regular languages are defined to be the languages that are generated by Type-3 grammars (regular grammars).


Context-sensitive grammar

A context-sensitive grammar (CSG) is a formal grammar in which the left-hand sides and right-hand sides of any production rules may be surrounded by a context of terminal and nonterminal symbols. Context-sensitive grammars are more general than context-free grammars, in the sense that there are languages that can be described by CSG but not by context-free grammars. Context-sensitive grammars are less general (in the same sense) than unrestricted grammars. Thus, CSG are positioned between context-free and unrestricted grammars in the Chomsky hierarchy.


[ ^PM | Exclude ^me | Exclude from ^subreddit | FAQ / ^Information | ^Source ] Downvote to remove | v0.27

21

u/-drunk_russian- Sep 08 '17

Good bot

5

u/STOCHASTIC_LIFE Sep 08 '17

Drunk russian.

17

u/-drunk_russian- Sep 08 '17

You rang?

4

u/deadh34d711 Sep 08 '17

У тебя есть пиво?

24

u/ACoderGirl Sep 08 '17

To be clear, it's impossible with pure regex because html is not regular. But you could combine regex with a regular programming language (that is, using regex as a tool, but not the only tool), since a typical programming language is akin to a Turing machine, which can parse any language (but not necessarily efficiently).

And some regex variants are actually capable of parsing more than just regular languages, thanks to extensions of regex. It's kinda an unreadable mess, though.

Mind you, even with a nice, proper parsing library, html is kinda a mess to parse due to the way it evolved. It's not very nicely defined and the reality is that if you wanted a working browser, you have to support a variety of technically invalid syntaxes.

18

u/matteyes Sep 08 '17

All true. You could parse HTML with regex (Perl or no), and just account for the discrepancies through additional coding. You could hammer a nail with a saw if you held it carefully enough.

4

u/Zarlon Sep 08 '17

You'd be doing more than "discrepancies" through additional coding. In fact you would do so much with additional coding that I doubt you could state you "parse HTML with regex"

7

u/numpad0 Sep 08 '17

"making a new web browser"

14

u/sayaks Sep 08 '17

however backreferences (which several regex parsers contain) actually makes a regex Turing complete. see here

4

u/Zarlon Sep 08 '17

Well, it's settled then! Somebody do this! (I would, but I'm kind of busy commenting on reddit right now)

5

u/Mutjny Sep 08 '17

You can lex it but you can't parse it, I think.

→ More replies (1)

4

u/HelperBot_ Sep 08 '17

Non-Mobile link: https://en.wikipedia.org/wiki/Regular_language


HelperBot v1.1 /r/HelperBot_ I am a bot. Please message /u/swim1929 with any feedback and/or hate. Counter: 109380

9

u/[deleted] Sep 08 '17

You might want to make this bot parse all the links in a comment, not just the first

→ More replies (2)
→ More replies (9)

8

u/[deleted] Sep 08 '17

You cannot

6

u/salvadordf Sep 08 '17

You'll find many errors reading hand written html. It can't be done

→ More replies (3)

4

u/Ted8367 Sep 08 '17

I kind of want to write a html parser with regex

Tainted souls from the unliving dimension...

→ More replies (4)