Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for finding a regular expression?

Tags:

regex

grep

Does anyone have code for finding a file that contains a regular expression? I would assume you could have two different flavors, one for BREs and one for EREs.

You would think some kind of test suites would have something like an isRegex() test. Can anyone have any code? Looking for something comprehensive of course.

I see this was discussed here but didn't see any practical responses. If I want to grep for any file that contains a regular expression, perhaps bounded by the typical //, how would I do it?

like image 718
tkotitan Avatar asked Mar 23 '09 20:03

tkotitan


People also ask

How do you find a regular expression?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

Is there a regular expression to detect a valid regular expression?

No, if you are strictly speaking about regular 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 all and only regexes.


1 Answers

Regular expressions are themselves not a regular language. The clue is that they contain parentheses and square brackets and such that must be balanced.

A regular expression itself can be described by a context-free grammar, and parsed with a recursive-descent parser.

like image 159
Bill Karwin Avatar answered Oct 07 '22 03:10

Bill Karwin