From: Robert Klemme Date: 2005-02-10T07:55:05+09:00 Subject: Re: How to mimic Perl's `s///' in Ruby? "Jos Backus" schrieb im Newsbeitrag news:20050209201021.GA47301@lizzy.catnook.com... > Given Perl's > > $_ = "123 foo"; > > s/^(\d+)\s+//; > $pid = $1; > > print "$_\n$pid\n"; > > all I can come up with is > > line = "123 foo" > > pid = nil > line.sub!(/^(\d+)\s+/) {pid = $1; ''} > > puts line, pid > > Is there a better way perhaps? Others have shown ways to mimic Perl - even so far as to include the trailing semicolons. I don't know why you want to mimic Perl, but here's how I'd do it in Ruby: if /^(\d+)\s+(.*)$/ =~ str pid, name = $1, $2 else # no match end Or, if you expect it to always match: raise "String error: #{str}" unless /^(\d+)\s+(.*)$/ =~ str pid, name = $1, $2 Kind regards robert