Is is possible to detect a valid regular expression with another regular expression? If so please give example code below.
|
This is a recursive regex, and is not supported by many regex engines. PCRE based ones should support it. Without whitespace and comments:
Edit: Added some description. |
|||||||||||||||||||||
|
Unlikely. Evaluate it in a |
|||||||||||||||||||||
|
No if you are strictly speaking about reguralr expressions and not including some regular expression implementations that are actually context free grammars. There is one limitation of regular expressions which makes it impossible to write a regex that matches a regex. You cannot match implementations such as braces which are paired. Regex's use many such constructs, lets take [] as an example. Whenever there is an [ there must be a matching ]. Simple enough for a regex "[.*]". What makes it impossible for regexe's is that they can be nested. How can you write a regex that matches nested brackets? The answer is you can't without an infinitely long regex. You can match any number of nested parens through brute force but you can't ever match an arbitrarily long set of nested brackets. This capability is often referred to as counting (you're counting the depth of the nesting). A regex by definition does not have the capability to count. EDIT: Ended up writing a blog post about this: Regular Expression Limitations |
|||||
|
Good question. True regular languages can not decide arbitrarily deeply nested well formed parenthesis. Ie, if your alphabet contains '(' and ')' the goal is to decide if a string of these has well-formed matching parenthesis. Since this is a necessary requirement for regular expressions the answer is no. However: if you loosen the requirement and add recursion you can probably do it. The reason is that the recursion can act as a 'stack' letting you 'count' the current nesting depth by pushing onto this stack. Russ Cox has written a wonderful treatise on regex engine implementation: Regular Expression Matching Can Be Simple And Fast |
|||||
|
Though it is perfectly possible to use a recursive regex as MizardX has posted, for this kind of things it is much more useful a parser. Regexes were originally intended to be used with regular languages, being recursive or having balancing groups is just a patch. The language that defines valid regexes is actually a context free grammar, and you should use an appropriate parser for handling it. Here is an example for a university project for parsing simple regexes (without most constructs). It uses JavaCC. And yes, comments are in Spanish, though method names are pretty self-explanatory.
|
|||||||||||||
|
This example on the pyparsing wiki gives a grammar for parsing some regexes, for the purposes of returning the set of matching strings. As such, it rejects those re's that include unbounded repetition terms, like '+' and '*'. But it should give you an idea about how to structure a parser that would process re's. |
|||
|
You can submit the regex to preg_match which will return false if the regex is not valid. Don't forget to use the '@' to suppress error messages:
|
|||
|
This class does the trick in PHP:
|
|||
Construct the validator either for a single regular expression or a set (array) of regular expressions. By default validation is case sensitive but constructors are provided to allow case in-sensitive validation. For example to create a validator which does case in-sensitive validation for a set of regular expressions:
Validate true or false: boolean valid = validator.isValid(value); Validate returning an aggregated String of the matched groups: String result = validator.validate(value); Validate returning the matched groups: String[] result = validator.match(value); (For using above code you can use apache validator) |
|||
|