# Gsub?

**URL:** <https://www.ruby-forum.com/t/gsub/113558>\
**Category:** Ruby\
**Created:** [September 1, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/gsub/113558 "2007-09-01T19:30:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [September 1, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/gsub/113558/1 "2007-09-01T19:30:34Z")

</div>

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…

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

2- if srt is of this type, then replace the number nnnnn by another one  
ppppp

don’t know how to do that in one DRY command…

thanks to the gsub guru …

btw : what’s the best link to learn more abour gsub… ?

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [September 1, 2007, 7:53pm UTC](https://www.ruby-forum.com/t/gsub/113558/2 "2007-09-01T19:53:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [September 2, 2007, 8:31am UTC](https://www.ruby-forum.com/t/gsub/113558/3 "2007-09-02T08:31:25Z")

</div>

On 2007-09-01 19:53:01 +0200, Sebastian H.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> > 1- detect that srt is a string of this type /cities/ nnnnn  
> > new\_number=$1.to\_i \* 2 # (Or however the new number is calculated)  
> > What you want to learn more about are regular expressions.  
> > You can do that at at www.regular-expressions.info
> 
> HTH,  
> Sebastian

Thanks a lot you made my SUnday !!
