From: Krishna Dole Date: 2003-04-24T08:45:47+09:00 Subject: Re: grep and regular expressions in ruby Thanks Bermejo and Daniel. It would be nice to have negation in REs and a more flexible grep, but I will get by fine using select. I've been meaning to purchase The Ruby Way... maybe tonight. Thanks again, Krishna -----Original Message----- From: Daniel Carrera [mailto:dcarrera@math.umd.edu] Sent: Wednesday, April 23, 2003 4:28 PM To: ruby-talk ML Subject: Re: grep and regular expressions in ruby On Thu, Apr 24, 2003 at 08:17:37AM +0900, Bermejo, Rodrigo wrote: > > ar.select {|e| e =~ /cat|small/ } > > > ar.select {|e| e =~ /cat/ or e != /small/ } > > rubyway? The latter is not an RE match: >> "cat" != /z/ ?> (i.e. Ruby will give you an error). I guess you can write: ar.select {|e| e =~ /cat/ or not e =~ /small/ } I don't know if this is the Ruby-way or not, but I do think that Ruby's RE's and grep are lacking. There should be a way to express negation in a regular expression, and there should be a way to express several conditions in grep. I guess that the complaint over grep really reduces to a complaint over REs. IMHO one should be able to type some sort of equivalent to "/cat/ and not /small/". Just like we have "/cat|small/". I have found myself wishing for something like that more than once. I can see that the exact syntax I wrote can't be made to work, but we can probably find an alternate RE-like syntax. Perhaps something like: /cat|small/ -> true if matches /cat/ or matches /small/ /cat&small/ -> true if matches /cat/ and matches /small/ /cat|!small/ -> true if matches /cat/ or does not match /small/ /cat&!small/ -> true if matches /cat/ and does not match /small/ Just my $0.02 > 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/) > > > > ar.select {|e| e =~ /cat|small/ } > > > > > >is there a more elegant solution? > > > >The second question concerns regexps: how do you indicate that you don't > >want to match the specified pattern? For example, if I only wanted strings > >containing "cat" but not containing "small". Once again, I think the perl > >would look like this: > > ar2 = grep(( /cat/ and !/small/ ), ar ); > > > > > > > >but '!' doesn't work for me in ruby. So far I haven't found anything about > >this in Pickaxe or on the web. > > > >thanks, > >Krishna > > > > > > > > > > -- > -------------------------------------------------------- > Rodrigo Bermejo | rodrigo.bermejo@ps.ge.com > IT-Specialist | 8*879-0644 > -------------------------------------------------------- > > > > -- Daniel Carrera Graduate Teaching Assistant. Math Dept. University of Maryland. (301) 405-5137