Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to parse regex strings with a regex

Just out of curiosity, is it possible to parse a string that is totally made out of random but valid regular expressions with a single regular expression?

given the string of regex:

<[^>]*>\xA9

parses to:

<[^>]*>
\xA9

in which the first one match html and second one match a copyright symbol.

Edit: I found a similar question asked at SO claiming that it maybe possible. Here, I'm referring to regex in JavaScript ECMA-262 only.

like image 517
Ray Cheng Avatar asked May 07 '12 02:05

Ray Cheng


1 Answers

No, it is not possible: regular expression language allows parenthesized expressions representing capturing and non-capturing groups, lookarounds, etc., where parentheses must be balanced. It is not possible even in theory to write a regular expression that verifies if parentheses are balanced in a given string. Without an ability to do that you wouldn't know where one regexp ends and the other one starts.

In general, regex grammar is relatively complex. To get an idea of just how complex it is, take a look at the parser in the source of Java's Pattern class.

like image 193
Sergey Kalinichenko Avatar answered Oct 11 '22 22:10

Sergey Kalinichenko