string replacement...

J

Josselin

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
 
S

Stefano Crocco

Alle venerd=EC 27 aprile 2007, Josselin ha scritto:
I have a string : str =3D "/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=
=20
do:

str.sub /\d+$/, '9999'

This will replace the text matching the regexp (i.e, one or more digit=20
followed by the end of the line) with the given string (note that this won'=
t=20
change the original string, but return a new one. If you need to change the=
=20
original one, use sub! instead of sub).

I hope this helps

Stefano
 
B

Björn Paetzel

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

How about:

str.gsub! "31551", "9999"

or to replace "the number at the end":

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

:)
 
D

Dan Zwell

Josselin said:
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.gsub!(/31551/, "9999")

good luck,
dan
 
W

William James

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"
 
A

Alex Young

William said:
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"
Whoa. I hadn't seen that syntax before. Nice.
 
R

Robert Klemme

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. :)

Kind regards

robert
 
J

Josselin

Alle venerdì 27 aprile 2007, Josselin ha scritto:

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"

;-)))
 
J

Josselin

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

How about:

str.gsub! "31551", "9999"

or to replace "the number at the end":

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

:)

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 :))
 
J

Josselin

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"

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
 
J

Josselin

William said:
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"
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
 
J

Josselin

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. :)

Kind regards

robert

I forgot to mention my main objective > replacing the term after the last '/'..
thanks to all

Joss
 
R

Robert Klemme

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. :)

Kind regards

robert

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
 
S

Sebastian Hungerecker

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

Robert Dober

On 4/27/07 said:
thanks Bj=F6rn , 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
irb(main):007:0> str =3D "/proposals/list/31555"
=3D> "/proposals/list/31555"
irb(main):012:0> str.gsub! /[0-9]+$/, "9999"
=3D> "/proposals/list/9999"

but

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


josss :))


--=20
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top