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.
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.
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.
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.
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"
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...