gsub ?

J

Josselin

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... ?
 
S

Sebastian Hungerecker

Josselin said:
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
 
J

Josselin

Josselin said:
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

Thanks a lot you made my SUnday !!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top