String replacement

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

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

Josselin wrote:

str.gsub!(/31551/, “9999”)

good luck,
dan

On Apr 27, 2:29 am, Josselin [email protected] wrote:

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

str[/\d+/] = “9999”

William J. wrote:

joss

str[/\d+/] = “9999”
Whoa. I hadn’t seen that syntax before. Nice.

On 2007-04-27 09:36:31 +0200, Stefano C. [email protected]
said:

joss
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”

;-)))

On 27.04.2007 09:29, Josselin wrote:

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 ?

How about

irb(main):006:0> str = “/proposal/list/31551”
=> “/proposal/list/31551”
irb(main):007:0> str[%r{\d+$}]=“9999”
=> “9999”
irb(main):008:0> str
=> “/proposal/list/9999”

If it’s a file name you could also do

irb(main):012:0> File.join(File.dirname(str), “9999”)
=> “/proposal/list/9999”

I estimate there are another 5 million other ways around. :slight_smile:

Kind regards

robert

On 2007-04-27 09:39:04 +0200, Dan Z. [email protected] said:

joss

str.gsub!(/31551/, “9999”)

good luck,
dan

thanks Dan, but I looked for more generic replacement… as Stefano
mentionned

joss

On 2007-04-27 09:52:29 +0200, William J. [email protected] said:

joss

str[/\d+/] = “9999”

irb(main):007:0> str = “/proposals/list/31555”
=> “/proposals/list/31555”
irb(main):008:0> str.sub /\d+$/, ‘9999’
=> “/proposals/list/9999”
irb(main):009:0> str[/\d+/] = “9999”
=> “9999”

got it from Stefano… Thanks Bill

On 2007-04-27 11:15:15 +0200, Robert K. [email protected]
said:

irb(main):006:0> str = “/proposal/list/31551”

I estimate there are another 5 million other ways around. :slight_smile:

Kind regards

robert

I forgot to mention my main objective > replacing the term after the
last ‘/’…
thanks to all

Joss

On 2007-04-27 10:01:07 +0200, Alex Y. [email protected] said:

joss

str[/\d+/] = “9999”
Whoa. I hadn’t seen that syntax before. Nice.

irb(main):007:0> str = “/proposals/list/31555”
=> “/proposals/list/31555”
irb(main):008:0> str.sub /\d+$/, ‘9999’
=> “/proposals/list/9999”
irb(main):009:0> str[/\d+/] = “9999”
=> “9999”

don’t give the expected result… Stefano’s way did it … ;-))

thanks Alex

On 27.04.2007 15:18, Josselin wrote:

value ?
If it’s a file name you could also do
I forgot to mention my main objective > replacing the term after the
last ‘/’…
thanks to all

In that case I’d use the File approach or do str[%r{[^/]+$}]=“9999”

robert

On 4/27/07, Josselin [email protected] wrote:

thanks Björn , expected result for digits, but I forgot any other
characters , replacing the item after the last / was my goal
Ah that gives the problem a different flavor:

(str.split(“/”)[0…-2] + [“9999”]).join(“/”)

but regexens are probably better after all

str.sub!(%r{[^/]*$}, “9999”)

HTH
Robert

Josselin wrote:

irb(main):009:0> str[/\d+/] = “9999”
=> “9999”

The return value of the assignment is “9999”, but the value of str will
be the
whole string with the 9999 at the end, so exactly what you wanted.

On 2007-04-27 09:37:25 +0200, Björn Paetzel [email protected] said:

or to replace “the number at the end”:

str.gsub! /[0-9]+$/, “9999”

:slight_smile:

thanks Björn , expected result for digits, but I forgot any other
characters , replacing the item after the last / was my goal

irb(main):007:0> str = “/proposals/list/31555”
=> “/proposals/list/31555”
irb(main):012:0> str.gsub! /[0-9]+$/, “9999”
=> “/proposals/list/9999”

but

irb(main):013:0> str2 = “/proposals/list/abcde”
=> “/proposals/list/abcde”
irb(main):014:0> str2.gsub! /[0-9]+$/, “9999”
=> nil

josss :-))