Is it possible to detect a valid regular expression with another regular expression? If so please give example code below.
Is it possible to detect a valid regular expression with another regular expression? If
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is a recursive regex, and is not supported by many regex engines. PCRE based ones should support it.
Without whitespace and comments:
.NET does not support recursion directly. (The
(?1)and(?R)constructs.) The recursion would have to be converted to counting balanced groups:Compacted:
From the comments:
It will validate just the regex part of substitutions and translations.
s/<this part>/.../It is possible if the regex engine supports recursion, such as PCRE, but that can’t really be called regular expressions any more.
‘In theory, theory and practice are the same. In practice, they’re not.’ Almost everyone who knows regular expressions knows that regular expressions does not support recursion. But PCRE and most other implementations support much more than basic regular expressions.
This pattern exploits an extension called recursive regular expressions. This is not supported by the POSIX flavor of regex. You could try with the -P switch, to enable the PCRE regex flavor.
This is true for classical regular expressions. Some modern implementations allow recursion, which makes it into a Context Free language, although it is somewhat verbose for this task.
[^?+*{}()[\]\\|]will match any single character, not part of any of the other constructs. This includes both literal (a–z), and certain special characters (^,$,.).