tags:

views:

333

answers:

7

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

+2  A: 

It is not possible using standard regular expressions.

Regular expressions can be nested indefinitely (eg, /(a(b(c(d))))/), which is impossible to match using standard regex.

SLaks
+15  A: 

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

Update: More useful practical answer

You cannot detect valid regular expressions using any regular expression. To detect its validity, you should just parse the string using the regex library and it would fail if it is an invalid regular expression. For example, in Java, it would be something like:

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

This technique should work with almost any language.

notnoop
That won't stop people from trying though.
Mark Byers
+1 - this is the essence of the answer to the question.
ConcernedOfTunbridgeWells
+1 I have to wonder if stackoverflow's tag line should be "Because regex can only parse regular languages"
middaparka
Todays regex (PCRE, etc) are not regular and can match unregular languages.
Qtax
@Qtax: Don't go there. Please.
Tim Pietzcker
A: 

Yes. Example: This regex ^[a-z][+*]$ will match this regex z+ and this a* and this c+ and so on.

Y. Shoham
A: 

This is not possible. Regular expressions can only match regular languages. Regular expressions are not a regular language. If memory serves I believe they are a context-free language and require a context-free grammar to match.

JaredPar
PCRE regex and the like are not regular.
Qtax
+7  A: 

You're all wrong! In my secret laboratories, my evil scientists have discovered the regular expression that can match any regular expression:

.*

It will even match the null expression. Let's see you try to match that!

As an added benefit, it will even match strings that are not regular expressions.

Carl Smotricz
I lol'd at the last sentence.
musicfreak
I didn't dare put this down as an answer, so I went for the cowardly comment. +1 to you!
Tim Pietzcker
I think Jeff, somewhere, encouraged us to have some fun once in a while. Hell, apart from the job market scam on the site next door, that's about the only return we're getting for our efforts.
Carl Smotricz
Funny, witty, but far from helpful.
Tom
@Tom: I help people with sensible questions. Since I'm not being paid here, I compensate myself by poking a bit of fun at the less sensible ones. And of course humor-impaired people like yourself.
Carl Smotricz
@Carl: I do not see how my question is not sensible. Indeed notnoop gives a perfectly legitimate answer. Before asking it, I did not know about regular languages, and the question seemed pretty reasonable. (BTW I'm not complaining about your humorous answer, but about your bitter comment)
Andrea
@Andrea: I apologize for calling your question "not sensible," and take it back. I seem to have missed the part where you are careful to ask "is it possible..."
Carl Smotricz
@Carl: apologies accepted ;-)
Andrea
A: 

Here we go:

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

Should match a regular expression delimited by //.

Of course, it won't ensure that the regular expression parses correctly - it just identifies where it is (say, for a tokenizer).

Anon.
Hmmmm, a regex that won't even match itself; I'm not exactly overflowing with confidence here. :P
Alan Moore
Well, if I rewrote it as `/\/([^\\\/]++|\\.)\//` it would. I could work on one to match any `m//` delimiter set if you really wanted.
Anon.
No need for that, we'll just make everybody in the world do what you did: rewrite their regexes so they match your regex. :D (BTW, you need to add another quantifier, this one on the whole group.)
Alan Moore
A: 

I have answered that question here.

T.E.D.