From: David Alan Black Date: 2002-04-11T12:19:32+09:00 Subject: Re: RegExp for "AND" search pattern... Hello -- On Thu, 11 Apr 2002, Chris wrote: > In article <3b3ad3b4.0204101533.638f26d6@posting.google.com>, > patrick-may@monmouth.com (Patrick May) wrote: > > > How could a regexp be constructed for an "AND" search pattern? > > How about... > > class String > def re_and_search ( *patterns ) > patterns.flatten! > patterns.collect! do |x| > x.type == "Regexp" ? x : Regexp.new(Regexp.escape(x.to_s)) > end > all_match = true > for x in patterns > unless self =~ x > all_match = false > break > end > end > all_match > end > end Let Ruby do more of the work for you :-) For example: class String def re_and_search(patterns) if patterns.detect {|pat| self !~ pat} false else true end end end or, if a return of nil or a non-empty array is OK, you could do just this: class String def re_and_search(patterns) patterns.detect {|pat| self !~ pat} end end (give or take some flattening :-) David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav