Re:In search of the perfect URL validation regex (2010)
by aranchelk on Saturday September 25, 2021 @07:36PM
(#28657065)
Caveats: I know nothing of Chomsky Grammars, and I have only a passing familiarity with Cthulu, but IMO the real crux of the issue parsing html with regex (beyond all the “it’s hard”, “the spec is more complicated than you think”, “regex is impossible to read” etc.) is html is a recursive data structure, e.g. you can have a div, inside a div, inside a div ad infinitum. Regex, AFAIK, doesn’t allow you to describe recursion, so you’re left with regex plus supporting code. You’ll then have an impedance mismatch between the two.
URLs are not recursive structures, so I’d say the single hardest feature of html is not present.
Re:In search of the perfect URL validation regex (2010)
by prox on Sunday September 26, 2021 @04:51AM
(#28659384)
The times I had to use it on HTML , I think I combined xPath with RegEx to close the mismatch.
Re:In search of the perfect URL validation regex (2010)
by MathMonkeyMan on Saturday September 25, 2021 @07:19PM
(#28656986)
I haven't looked at the BNF(s) for URIs lately, but I don't recall there being any recursion, so I wouldn't be surprised if the language were regular.
There was a Perl program that would take something like a BNF and barf out a gigantic regex (maybe with some maximum depth).
Re:In search of the perfect URL validation regex (2010)
by tester756 on Saturday September 25, 2021 @08:20PM
(#28657282)
>So, does this apply to URLs? The fact that these regexes are....so huge...makes me think that something is fundamentally wrong
Yes, if your regex is above {.../50/100/...} characters, then write parser.
I struggle to understand why do people write those crazy regexes for emails, urls, html when probably in all popular technologies there are battle-tested parsers for those things.
Re:In search of the perfect URL validation regex (2010)
by Too on Sunday September 26, 2021 @03:07AM
(#28659007)
On top of this the error messages with a regex will be very one-dimensional.
As an example, http://localhost/ is technically valid url, which he wants to block. Should this error say misformatted URL like all others?
Using regex to cover all such cases is really the wrong tool for the job.
Re:In search of the perfect URL validation regex (2010)
by staticassertion on Saturday September 25, 2021 @08:34PM
(#28657368)
Sometimes you're given an arbitrary bag of bytes with best-effort well-formed data. Regexes are gross but quite good for those cases where you need to try to rip out some bits from the data abyss.
So, does this apply to URLs? The fact that these regexes are....so huge...makes me think that something is fundamentally wrong. Are URLs describable in a Chomsky Type 3 grammar? Are they sufficiently regular that using a Regex is sensible? What do the actual browsers do?
[1] https://stackoverflow.com/questions/1732348/regex-match-open...