From: Tim Hunter Date: 2003-06-11T03:07:09+09:00 Subject: Re: OT: Regexp question On Wed, 11 Jun 2003 01:18:01 +0900, Michael Campbell wrote: >--- Laurent Sansonetti wrote: >> Hello Dominik, >> >> Dominik Werder wrote: >> > This is probably not the easiest way to solve this: >> > file.gets=~/(.+?)( \(.+)?$/ >> > puts $1 >> > >> > How can I make it easier? >> > >> >> laurent@bigmac ~> irb --simple-prompt >> >> a = [ "Illinois (3762)", "Indiana", "Iowa (64)", "Kansas", >> "Kentucky >> (2675)" ] >> => ["Illinois (3762)", "Indiana", "Iowa (64)", "Kansas", "Kentucky >> (2675)"] >> >> r = /(\w+)/ >> => /(\w+)/ >> >> a.each { |s| p r.match(s)[0] } >> "Illinois" >> "Indiana" >> "Iowa" >> "Kansas" >> "Kentucky" >> >> Hope this helps, > > >Add "South Dakota (00)", "New Jersey (29332)" to your tests... =) > a = [ "Illinois (3762)", "Indiana", "Iowa (64)", "Kansas", "Kentucky (2675)", "South Dakota (00)", "New Jersey (29332)" ] a.each { |s| p s.split(/ \(/)[0] }