From: Florian Gross Date: 2005-02-10T06:25:06+09:00 Subject: Re: How to mimic Perl's `s///' in Ruby? Glenn Parker wrote: > Closer to the original Perl: > > line = "123 foo" > line.sub!(/^(\d+)\s+/, '') > pid = $1 > puts line, pid line = "123 foo" pid = line.slice!(/\d+/, 1).to_i puts line, pid ..slice!() is the in-place version of [] that will delete the matched stuff as well as returning it. Note that you can use Regexps with [] and even []=. str = "hello world" str[/.(.)/, 1] # => "e" str[/\s(.+)/, 1] = "bar" str # => "hello bar"