开发者

what is the regex pattern for a regex pattern?

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 rege开发者_如何学编程x pattern. What is the best pattern to use to determine this?

Thanks, Anand


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


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.


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)*)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜