Live data from Hacker News

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

stackoverflow.com

41–50 of 224 posts

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

#42

"Evaluate it in a try..catch or whatever your language provides." "That's not very enterprisey of you" Oooh, I'm laughing so hard it hurts. It's been a particularly 'enterprisey' week at work.

Actually that is how enterprise works afaik. But few well funded startups on the other hand...

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

#43
An interesting question would be "which regex variant can be parsed by itself, and how?".

Parsing simple regex with PCRE is cheating. If you are using PCRE, you should be parsing PCRE.

The simplest case would be DOS style globs with just * and ?, and it can parse itself: just use "*".

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

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

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

#45
post #38
post #17

Earlier quoted context omitted.

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

If you were looking for the reason why a regex cannot parse HTML, it is because HTML has matching nested tags and regex parsers are finite state machines (FSM). What this means is that a regex parser is like a goldfish. It only knows about the state it is currently in (what it just read) and which possible states it may transition to (what is legally allowed to come next). The fish never remembers where it was before…

The question is about identifying end-tags in XHTML. This is indeed possible with a regex.

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

#46
post #18

This immediately triggers the Liar's paradox [1]. Can a regular expression check its own validity? How can you tell if it's lying? Beware of bugs in the above code; I have only proved it correct, not tried it --Donald Knuth [1] https://en.wikipedia.org/wiki/Liar_paradox

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

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

#47
post #33
post #27

Earlier quoted context omitted.

That does not really apply. A compiler can compile itself, it can check if its own code is valid.

You are necessarily claiming that a compiler is incapable of false negatives. e.g incorrectly validating its own code. Beware of bugs in the above code; I have only proved it correct, not tried it --Donald Knuth

The fact that someone might make a mistake in doing something does not show it cannot be done. More generally, You seem to be mistaking validating a program's source with the question of whether it performs its intended purpose. These are different things, and attempting to conflate them will only lead to confusion.

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

#48
post #28
post #22

Earlier quoted context omitted.

Had it not gotten extremely lucky, it would have a very negative score. Anything intended to be funny on StackOverflow doesn’t go over well very often.

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?

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

#49
post #46
post #18

This immediately triggers the Liar's paradox [1]. Can a regular expression check its own validity? How can you tell if it's lying? Beware of bugs in the above code; I have only proved it correct, not tried it --Donald Knuth [1] https://en.wikipedia.org/wiki/Liar_paradox

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

Post reply on HN