Regular expression that matches regular expressions

Is it possible to write a regular expression that matches regular expressions? Anyone have any examples? If there is some theoretical obstruction, does anyone know of a regex that matches at least the most common regex patterns?

+3
source share
7 answers

Regular expressions are not a regular language and therefore cannot be described by a regular expression!

Update : more useful practical answer

, . , , , , . , Java - :

boolean isValidRegexp(String s) {
  try {
    Pattern.compile(s);
    return true;
  } catch (Exception e) {
    return false;
  }
}

.

+17

! , :

.*

. , !

, .

+8

.

(, /(a(b(c(d))))/), .

+2

Crockford , ( , JavaScript)

/\/(\\[^\x00-\x1f]|\[(\\[^\x00-\x1f]|[^\x00-\x1f\\\/])*\]|[^\x00-\x1f\\\/\[])+\/[gim]*/
+1

. : ^[a-z][+*]$ z+ a* c+ ..

0

It's impossible. Regular expressions can only correspond to common languages. Regular expressions are not a common language. If memory works, I believe that they are a context-free language and require matching contextual grammar.

0
source

Here we go:

m{/([^\\/]++|\\.)/}

Matches regex limited //.

Of course, it does not guarantee the correct analysis of the correct expression - it simply determines where it is (say, for the tokenizer).

0
source

Source: https://habr.com/ru/post/1728518/


All Articles