From: "Bermejo, Rodrigo" Date: 2003-04-24T09:03:31+09:00 Subject: Re: grep and regular expressions in ruby I widely recommend you (if you are a newbie) the 'Sam's Teach Yourself Ruby in 21 days ' book..... a good and enjoyable reading .... I read it in just 18 days(..may I forgot some chapters ?) ....and it's still in my desktop Take a look at: http://www.bookpool.com/.x/mprew9z9y1/ss/1?qs=ruby They use to have good prices. -r. Novody is perfect. ------------------> Krishna Dole wrote: >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 >>> >>> >>> >>> >>> >>> >> >> >> > >-- >Daniel Carrera >Graduate Teaching Assistant. Math Dept. >University of Maryland. (301) 405-5137 > > > > > -- -------------------------------------------------------- Rodrigo Bermejo | rodrigo.bermejo@ps.ge.com IT-Specialist | 8*879-0644 --------------------------------------------------------