From: Sebastian Hungerecker Date: 2007-09-02T02:53:01+09:00 Subject: Re: gsub ? Josselin wrote: > I don't manage very well the gsub function, not too bad with simple > one, but when it comes to string containing slashes, I totally mess it.. Use %r{your_regex} instead of /your_regex/ - then you don't have to escape the slashes. > my string can be srt = "/cities/31550/posts;search" > > and I would like > 1- detect that srt is a string of this type /cities/ nnnnn > /posts;search" where nnnnn is a number between 1 and 99999 %r{/cities/(\d{1,5})/posts;search} \d{1,5} is "one to five digits" and the parens tell it to remember that as group 1. > 2- if srt is of this type, then replace the number nnnnn by another one > ppppp srt.sub(%r{/cities/(\d{1,5})/posts;search}) do new_number=$1.to_i * 2 # (Or however the new number is calculated) "/cities/#{new_number}/posts;search" end If the new number is fixed (i.e. it doesn't depend on the old number), you can just do: srt[%r{/cities/(\d{1,5})/posts;search},1]="102943" > btw : what's the best link to learn more abour gsub... ? There's not much to gsub itself. It's just string.gsub(re,str) or string.gsub(re) {|match| do_something; return a string} What you want to learn more about are regular expressions. You can do that at at www.regular-expressions.info HTH, Sebastian -- NP: In Flames - Dead Eternity Jabber: sepp2k@jabber.org ICQ: 205544826