From: Clement Ow Date: 2008-06-25T13:59:44+09:00 Subject: Re: Regex extracting certain characters Pe単a, Botp wrote: > From: Clement Ow [mailto:clement.ow@asia.bnpparibas.com] > # What I want to test for is actually just solely '/2008' which can be > # anywhere in the path name, maybe citing some examples would > # be clearer. > # These are the kind of strings i want to test for: > # C:/2008/ruby or C:/ruby/test/2008 or C:/ruby/test/2008/testing > # These are the kind of strings that I want to eliminate: > # C:/20080229/ruby or C:/ruby/test/20080502 or > # C:/ruby/test/20080405/testing or C:/29022008/ruby > # In other words, I want to just test for "/" followed by "2008" in a > # given string. > > the solutions given by Stefano are great. just try them. > > i can show some other ways, eg > > a > #=> ["C:/2008/ruby", "C:/ruby/test/2008", "C:/ruby/test/2008/testing", > "C:/20080229/ruby", "C:/ruby/test/20080502", > C:/ruby/test/20080405/testing", "C:/29022008/ruby"] > > a.grep /(^|\/)2008(\/|$)/ > #=> ["C:/2008/ruby", "C:/ruby/test/2008", "C:/ruby/test/2008/testing"] > > and again, another non-regex way, > > a.select{|path| path.split("/").any?{|x| x=="2008"}} > #=> ["C:/2008/ruby", "C:/ruby/test/2008", "C:/ruby/test/2008/testing"] > > btw, if you have "C:/ruby/2008/20080502", would you accept it? > hmmm, for this string I might want to exclude.. Any ideas how it can be done using Regexp? > it would also be nice if you could show sample code snippet of your > problem. remember, there are many ways ;) > > kind regards -botp Oh yea, cool ways there.. I used /(^|\/)2008(\/|$)/ in the end.. Thanks it works! =) -- Posted via http://www.ruby-forum.com/.