From: Josselin Date: 2007-04-27T22:15:11+09:00 Subject: Re: string replacement... On 2007-04-27 09:36:31 +0200, Stefano Crocco said: > Alle venerd� 27 aprile 2007, Josselin ha scritto: >> I have a string : str = "/proposal/list/31551" >> >> I would like to change the 31551 by another value "9999", but I don't >> see whicj method I should use ? >> >> str.each and block ? or str.rindex('/') and concatenating the new value ? >> >> tfyh >> >> joss > > If the text you need to replace is always at the end of the string, you can > > do: > > str.sub /\d+$/, '9999' > > This will replace the text matching the regexp (i.e, one or more digit > followed by the end of the line) with the given string (note that this won' > t > change the original string, but return a new one. If you need to change the > > original one, use sub! instead of sub). > > I hope this helps > > Stefano Thnaks Stefano , it gives the expected result irb(main):007:0> str = "/proposals/list/31555" => "/proposals/list/31555" irb(main):008:0> str.sub /\d+$/, '9999' => "/proposals/list/9999" ;-)))