tags:

views:

67

answers:

4

Is there are more succinct or Rubyesque way of writing this:

if ( variable =~ /regex1/ || variable =~ /regex2/ || variable =~ /regex3/ ... )
end

Namely, I'm hoping for something shorter, like:

if ( variable =~ /regex1/,/regex2/,/regex3/ )

which I realize is not valid Ruby code, but figuring someone may know a more clever trick.

+5  A: 

How about...

if ( variable =~ /regex1|regex2|regex3/ )
end
VoteyDisciple
+6  A: 
[/regex1/,/regex2/,/regex3/].any?{|r| r =~ variable}
p00ya
+2  A: 

You could use a switch, or merge the expressions (if that's possible), or use a find:

if ([/regex1/, /regex2/].find {|r| v =~ r}) ...
levi_h
+1  A: 
variable =~ Regexp.union(/regex1/, /regex2/, /regex3/)

This is assuming you can't use VoteyDisciple's which would make the most sense where it's possible.

sepp2k