Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regexp that matches valid regexps

Tags:

regex

Is there a regular expression that matches valid regular expressions?

(I know there are several flavors of regexps. One would do.)

like image 620
Thorsten79 Avatar asked Dec 12 '08 13:12

Thorsten79


People also ask

How do you check if a regex is valid or not?

new String(). matches(regEx) can be directly be used with try-catch to identify if regEx is valid. While this does accomplish the end result, Pattern. compile(regEx) is simpler (and is exactly what will end up happening anyway) and doesn't have any additional complexity.

How do you validate a regex pattern?

To validate a field with a Regex pattern, click the Must match pattern check box. Next, add the expression you want to validate against. Then add the message your users will see if the validation fails. You can save time for your users by including formatting instructions or examples in the question description.

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.

What does ?= Mean in regex?

?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured. Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).


2 Answers

Is there a regular expression that matches valid regular expressions?

By definion, it's quite simple: No.

The language of all regexes is no regular language (just look at nested parentheses) and therefore there can't be a regular expression to parse it.

like image 196
Dario Avatar answered Sep 21 '22 08:09

Dario


If you merely want to check whether a regular expression is valid or not, simply try to compile it with whichever programming language or regular expression library you're working with.

Parsing regular expressions is far from trivial. As the author of RegexBuddy, I have been around that block a few times. If you really want to do it, use a regex to tokenize the input, and leave the parsing logic to procedural code. That is, your regex would match one regex token (^, $, \w, (, ), etc.) at a time, and your procedural code would check if they're in the right order.

like image 34
Jan Goyvaerts Avatar answered Sep 21 '22 08:09

Jan Goyvaerts