Live data from Hacker News

Is there a regular expression to detect a valid regular expression?

stackoverflow.com

51–60 of 224 posts

Re: Is there a regular expression to detect a valid regular expression?

#51
post #17

Earlier quoted context omitted.

Perhaps the most epic reply I have ever seen on SO.

It’s a famous post on StackOverflow, but I don’t find it particularly helpful.

I found it extremely helpful. The sheer emphasis of the reply made me very curious why the idea of using regex on xml is so bonkers.

- I will never forget that regex can't parse XHTML

- the reason being, regex is insufficiently powerful

- when I first saw this post, I knew little about regex under the hood, this sent me down a wiki hole of FSMs, pushdown automata and turing machines

- this misconception is apparently common enough to be madness-inducing to those that know better

- use a hecking xml parser instead

It almost reminds me of a Bill Nye sketch. Teaching through a bit of non-sequitur and absurdism.

Re: Is there a regular expression to detect a valid regular expression?

#54
post #48
post #28

Earlier quoted context omitted.

The answer was made in '09. StackOverflow was more accepting of some humour at the time. And it is making a valid point: regex isn't the right tool for this job.

The question is about how to identify start and end-tags in XHTML. What would be an appropriate tool for that job?

A parser. Specifically, an XHTML parser.

Re: Is there a regular expression to detect a valid regular expression?

#55
post #49
post #46

Earlier quoted context omitted.

The "liar" only says "I can proofread Essays" and not "I am lying".

The "liar" is saying that the Essay is free of errors. (To the best of the "liar's" ability to detect them). The liar's knowledge is incomplete. https://en.wikipedia.org/wiki/G%C3%B6del%27s_incompleteness_...

Free of English errors. Not semantic errors. We don't even know it's a "liar".

Re: Is there a regular expression to detect a valid regular expression?

#56
post #55
post #49

Earlier quoted context omitted.

The "liar" is saying that the Essay is free of errors. (To the best of the "liar's" ability to detect them). The liar's knowledge is incomplete. https://en.wikipedia.org/wiki/G%C3%B6del%27s_incompleteness_...

Free of English errors. Not semantic errors. We don't even know it's a "liar".

Precisely, but as far as a compiler/interpreter is concerned syntax is semantics.

You can't codify moral judgments.

Re: Is there a regular expression to detect a valid regular expression?

#57

> Is there a regular expression to detect a valid regular expression? No, there is not. For example, parentheses in a regex must be balanced, and (famously) there is no regex to detect balanced parentheses.

Sure there is. Read the linked answer, or just google it.

Example:

https://regular-expressions.mobi/recurse.html?wlr=1

Re: Is there a regular expression to detect a valid regular expression?

#59
post #57

> Is there a regular expression to detect a valid regular expression? No, there is not. For example, parentheses in a regex must be balanced, and (famously) there is no regex to detect balanced parentheses.

Sure there is. Read the linked answer, or just google it. Example: https://regular-expressions.mobi/recurse.html?wlr=1

It depends if we're talking regex in the mathematical sense or in the programming one.

Re: Is there a regular expression to detect a valid regular expression?

#60
post #44
post #2

I believe Zalgo has the answer to this, via an equivalent question. https://stackoverflow.com/questions/1732348/regex-match-open...

Here is the answer to the question: https://www.cargocultcode.com/solving-the-zalgo-regex/ tl;dr: It can indeed be solved relatively easily with a regex.

This is a bit out of my wheelhouse, but this feels wrong, or at least naively capable. Like it feels like this sort of reasoning leads to the kind of bugs (depending on what you use the result of the rexex for) that allow for code injection, a la the Equifax hack.

Maybe another HN poster can back me up, or explain why in fact Zalgo is mistaken and CargoCode is correct.

Either way, this sort of complexity is one reason I avoid XML like the plague and keep HTML at arm's length.

Post reply on HN