Is it possible to detect a valid regular expression with another regular expression? If so please give example code below.
No, if you use standard regular expressions.
The reason is that you cannot satisfy the pumping lemma for regular languages. The pumping lemma states that a string belonging to language "L" is regular if there exists a number "N" such that, after dividing the string into three substrings x
, y
, z
, such that |x|>=1 && |xy|<=N
, you can repeat y
as many times as you want and the entire string will still belong to L
.
A consequence of the pumping lemma is that you cannot have regular strings in the form a^Nb^Mc^N
, that is, two substrings having the same length separated by another string. In any way you split such strings in x
, y
and z
, you cannot "pump" y
without obtaining a string with a different number of "a" and "c", thus leaving the original language. That's the case, for example, with parentheses in regular expressions.