2

What is the regex pattern for a regex pattern? I am using Ruby version 1.8.7, and have a requirement to determine if a given string is a regex pattern. What is the best pattern to use to determine this?

Thanks, Anand

1
  • I'm not 100% sure, but I think this has been asked before for Ruby regular expressions. Commented Aug 18, 2011 at 1:09

3 Answers 3

6

well any word can be a regex pattern. The string "cat" could be compiled to a regex pattern. You could do something like:

def is_regex?(str)
  Regexp.new(str) rescue false
end
Sign up to request clarification or add additional context in comments.

5 Comments

There are certain strings which are invalid - for instance, /(/ is not a valid regex for any regex engine where parentheses are special by default (which is most of them).
which is my point of doing a Regexp.new.. which will attempt a compile on the str or throw an exception if it cant compile and return false.. are you saying this wont work?
This is the only sane way to do this :)
@Jake no, I'm not - I was responding to the the "'is'" bit.
fair point.. I was using string loosely to say 'word' really.. but definitely any string is not a regex which is why the method would be needed to test for is_regex.
2

There isn't one, since regexes themselves aren't a regular language (due to things like matching parentheses).

If you want to determine whether something is a valid regex, I'd recommend just trying to compile it as a regex and see if it fails.

2 Comments

You're very confused. The things people call regular expressions are perfectly capable of match things that aren’t regular languages, including matching parentheses and such.
Nested parentheses are not something most regex flavors handle nicely. Yes, there are recursive regex engines (such as Perl's recursive reference), but they're really a poor choice in comparison to using an actual regex parser.
0

Assuming the regex pattern is a string with two forward slashes, / defining the pattern, then the regex to find a pattern with two foward slashes at the beginning and end of a pattern is as following:

(\/(?:.)*\/(?:i|g|s|m|U)*)

3 Comments

@David: All this regex is required to match is two forward-slashes; everything else is optional. That's not even sufficient to identify a regex literal, and regexes can also be written in the form of string literals (no slashes). It's not clear to me whether the OP is trying to locate regexes or validate them, but this regex does neither.
@Alan: I was just giving an example if the regex was defined literally as this was the only method I saw possible of finding a regex through regex.
@Alex: There are some problems with the regex, too. For example, to match one of several specific characters, a character class is more efficient and readable than an alternation--that is, your (?:i|g|s|m|U) should be [igsmU]. Except those modifiers aren't valid; in Ruby 1.8.7 (without the Oniguruma Gem), only [imox] are allowed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.