From: Dave Thomas Date: 2003-04-24T12:26:54+09:00 Subject: Re: grep and regular expressions in ruby Krishna Dole wrote: > I'm quite taken with ruby, but recently I ran into trouble using grep. I > have two questions: first, is there a way to call grep with multiple > regexps? For example, if you had > ar = ["cat", "dog", "smallcat"] > > and you only wanted strings matching both "small" and "cat", in perl I > think you could do something like > ar2 = grep(( /cat/ and /small/ ), ar ); > > but in ruby it seems like you have to call grep twice > ar2 = ar.grep(/cat/).grep(/small/) > > is there a more elegant solution? Have you had a look at lib/eregexp? It doesn't quite do what you need, but a couple of quick additions makes it work: require 'eregex' class RegOr def ===(other) self =~ other end end class RegAnd def ===(other) self =~ other end end ar = [ "cat", "dog", "catdog", "dogcat" ].grep(/cat/&/dog/) Cheers Dave