No, there is not. For example, parentheses in a regex must be balanced, and (famously) there is no regex to detect balanced parentheses.
Is there a regular expression to detect a valid regular expression?
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.
Re: Is there a regular expression to detect a valid regular expression?
#43Parsing 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?
#44I believe Zalgo has the answer to this, via an equivalent question. https://stackoverflow.com/questions/1732348/regex-match-open...
tl;dr: It can indeed be solved relatively easily with a regex.
Re: Is there a regular expression to detect a valid regular expression?
#45Earlier 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…
Re: Is there a regular expression to detect a valid regular expression?
#46This 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
Re: Is there a regular expression to detect a valid regular expression?
#47Earlier 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
Re: Is there a regular expression to detect a valid regular expression?
#48Earlier 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.
Re: Is there a regular expression to detect a valid regular expression?
#49This 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's knowledge is incomplete.
https://en.wikipedia.org/wiki/G%C3%B6del%27s_incompleteness_...