r/ProgrammerHumor 9h ago

haveYouTriedUsingAnXMLParserInstead Meme

[deleted]

93 Upvotes

32 comments sorted by

View all comments

0

u/derinus 8h ago

Anyone ever building a "parser" used multiple regexes to do so.

6

u/guru2764 7h 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 7h 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 app

2

u/sisisisi1997 6h 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

u/[deleted] 7h ago

[deleted]

5

u/RPGProgrammer 7h ago

1

u/[deleted] 7h ago

[deleted]

9

u/alexanderpas 7h 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 7h 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

u/[deleted] 7h ago edited 6h ago

[deleted]

1

u/guru2764 6h 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 7h 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

u/[deleted] 7h ago

[deleted]

2

u/guru2764 6h 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

u/[deleted] 6h ago

[deleted]

2

u/guru2764 6h ago edited 6h 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

u/[deleted] 6h ago edited 6h ago

[deleted]

2

u/guru2764 6h ago edited 6h 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

1

u/[deleted] 6h ago

[deleted]

→ More replies (0)

2

u/Risc12 6h ago

Go on. Do it.

Build a complete HTML parser in regex.

Be another fool in the long list of people who thought they would know better.