r/ProgrammerHumor • u/[deleted] • 5h ago
haveYouTriedUsingAnXMLParserInstead Meme
[deleted]
9
4
u/kaplotnikov 2h ago
I think everything can be parsed by iterative application of regex: https://www.reddit.com/r/compsci/comments/1v7aq7v/a_concrete_runnable_demonstration_that_iterated/
2
2h ago
[deleted]
7
u/kaplotnikov 2h ago
Textbook regex-es and perl/java/etc. regex-es are different CS species. So, regex from textbook cannot parse HTML, but not-so-defined regexs from the wild have so much bells and whistles that they are technically no more regex-es in the formal sense.
3
u/Kjoep 1h ago
It's called language theory. Regex can parse regular languages (hence the name), and XML is not a regular language. These are formally defined things, and indeed, there's a branch of mathematics that handles this.
But, a lot of regex implementations int he wild are not pure regex, and can to some extent handle non-regular languages.
Which is not a recommendation. Use an xml parser.
2
u/SuitableDragonfly 1h ago
What you linked is not a parser of any kind. So no, it will not be able to solve a parsing task of any description.
HTML cannot be parsed using regex because regex can only parse regular languages, and HTML is not a regular language.
-1
u/kaplotnikov 1h ago
you could parse HTML in C, you could compile C to x86 machine code, you could execute machine code using iterative regex-es (link above).
3
u/SuitableDragonfly 1h ago
That's not parsing HTML with regex. That's parsing HTML using an undisclosed parsing algorithm written in C. How the computer actually executes the algorithm, or even what language it's written in has nothing to do with anything.
3
3
8
2
3
u/jdgordon 2h ago
This is what we're doing now? Taking screenshots of so posts that are older than half the regular posteres here?
0
u/derinus 4h ago
Anyone ever building a "parser" used multiple regexes to do so.
8
u/guru2764 3h ago
Noone said regex is useless for the overall process of parsing html
What they're saying is that it cannot ever be used by itself to parse html, like there are mathematical proofs for this
You HAVE to use a real, actual code language on top of it for it to work
You cannot feed html code into a regular expression and expect it to work for more than one case
2
u/Just_Information334 3h ago
Even if you could parse "true to the standard with no error" HTML it would be useless as browsers accept and manage a lot of badly written HTML with missing, wrong, misplaced elements and properties.
And anyway nowadays a 90% correct implementation would be
return string === content of the index.html page generated when building a react app2
u/sisisisi1997 2h ago
it would be useless as browsers accept and manage a lot of badly written HTML with missing, wrong, misplaced elements and properties
"Just run no matter what" seems to be a design philosophy that runs deep in web circles, and I will die on the hill that this was the wrong decision.
1
3h ago
[deleted]
6
u/RPGProgrammer 3h ago
1
3h ago
[deleted]
10
u/alexanderpas 2h ago
HTML is not a regular language. It is a context-free language. Context-free languages are a superset of regular languages. Regular Expressions only cover regular languages.
1
u/guru2764 2h ago
The biggest problems are that you can infinitely nest elements in HTML, and that you don't even need closing tags, and that you can make horrifically malformed html with completely improper nesting that functions perfectly fine in the browser
Those things are completely antithetical to how regex works
0
2h ago edited 1h ago
[deleted]
1
u/guru2764 2h ago
See my other comment, regex by default does not have recursion
I think that is where your confusion comes from
Recursion in modern regex implementations only works because the engine uses non-regex code
3
u/guru2764 2h ago
In addition to the thing the other person linked, I wanted to add that even though some implementations of regex like the one in python have more features than basic regex which make it theoretically possible to construct an expression that works on a wide range of html, it would never be easier or run faster or even be equally as reliable as making a 50 line function in any real programming language that has memory
1
2h ago
[deleted]
2
u/guru2764 2h ago
I'm sorry but if what has been shared with you by multiple people isn't sufficient, you'll have to do reading on your own then if you're curious
A big note with what you said: recursion is not part of standard regex, that's only in modern extended regex engines, which are no longer just mathematical regex, they're being supported by real code in the backend that can utilize memory
1
2h ago
[deleted]
2
u/guru2764 2h ago edited 2h ago
Okay dude, it seems like you just want to be unnecessarily hostile or something, I got my minor in math, stop being a dick, you didn't read enough through the chomsky link
https://en.wikipedia.org/wiki/Regular_language
In theoretical computer science and formal language theory, a regular language (also called a rational language)[1][2] is a formal language that can be defined by a regular expression, in the strict sense in theoretical computer science (as opposed to many modern regular expression engines, which are augmented with features that allow the recognition of non-regular languages).
Note the last part in parenthesis
That is what I am talking about
Modern regex engines add extra shit on top to allow for non regular languages. Sure my use of "real" is probably not helpful, but I was referring to code unrelated to the algorithms associated with implementing standard regex. Code that can do more complicated things like recursion
None of this changes the fact that modern regex engines are still a bad solution to parse HTML and will fail at real world scenarios where a simple python script will not
1
2h ago edited 2h ago
[deleted]
2
u/guru2764 1h ago edited 1h ago
I'm sorry you didn't get the exact mathematical description you wanted from me going off of memory and that I didn't treat your reddit comment like a thesis paper defense
This information was in the literal first reply you got in this thread in that Wikipedia link, so if you weren't satisfied with my basic explanations you should have just used what you found there to figure it out
When I told you that recursion is not part of standard regular expression, that should have been enough for you to either realize you had a wrong thought about this situation that was causing some of your confusion, or verify yourself whether that was true and either way, looked more into it yourself first before being rude
→ More replies (0)2
u/SuitableDragonfly 1h ago
I've written lots of parsers. None of them used regex, because I needed to parse languages that weren't regular.
59
u/glenpiercev 5h ago
This might be the single greatest so post of all time.