Is it possible to have a regular expression matching all valid regular expressions?

Is it possible to determine if a given string is a valid regular expression using only regular expressions?

Let's say I have a few lines, which may or may not be valid regular expressions. I would like the regular expression to match the string matching the actual regular expression. Is it possible? Or am I using any higher level grammar (i.e. contextual free language) to detect this? Does it affect if I use an extended version of regular expressions like Perl regular expressions?

If possible, then what regexp regular expression matches?

+6
computer-science regex regular-language
source share
3 answers

No, It is Immpossible. This is because regular regular expressions include a grouping that requires balanced parentheses.

Balanced delimiters cannot match a regular expression; instead, they should be mapped to context-free grammar . (The first example in this article is about balanced parentheses.)

+8
source share

Watch an excellent post here:

Regular expression for regular expressions?

The answer is that regular expressions are NOT written using regular grammar, but without context.

+1
source share

If your question was “matches all valid regular expressions,” the answer (perhaps unexpectedly) is “yes.” The regular expression .* Matches all valid (and invalid) regular expressions, but is pretty useless for determining if you are really looking at the real one.

However, since the question is “matching all and only the correct regular expressions,” the answer (as DVK and Platinum Azure said “no”).

0
source share

All Articles