From: Hee-Sob Park Date: 2001-04-21T12:41:04+09:00 Subject: [ruby-talk:13954] Re: regexp "James Ponder" wrote in message news:20010421024536.C14119@snowy.squish.net... > Hi, I'm new to ruby and am coming from a perl background - therefore I > was hoping to use the perlisms in ruby when it came to regexps. > > Quite often you have an input line, and you want to pull data out from it > using a regexp. In perl I would do the following, naming my fields without > ever using $1, $2, etc.: > > if (($key, $val) = $var =~ /^(.+)=(.+)$/) { > # ... > } > > In ruby, the =~ String instance method returns the position of the match > (which perl does when in scalar context) rather than an array of matches, > so I'm finding it quite difficult to find a way to do the above perl > code, which in my mind is very readable and understandable. > > I thought about doing > > key, val = var =~ /^(.+)=(.+)$/ && $~[1..-1] > if key > ... > > but nobody would be able to read my code (besides I started feeling sick)... > > so at the moment I just do: > > if var =~ /^(.+)=(.+)$/ > key, val = $1, $2 > > but that's no where near as nice as perl's one-liner (and I really hate using > $1, $2 etc.). Any ideas? > > How about key, val = /^(.+)=(.+)$/.match(var)[1..-1] if key ... Or if (key, val = /^(.+)=(.+)$/.match(var)[1..-1])[0] ... > Best wishes, James > -- > James Ponder; www.squish.net Park Hee-Sob