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
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:
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
str[/\d+/] = "9999"
On Apr 27, 2:29 am, Josselin <josse...@wanadoo.fr> 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
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
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 ?
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 :-))
On 2007-04-27 09:37:25 +0200, Björn Paetzel <news@kolrabi.de> said:
Josselin schrieb:
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"
William James wrote:
On Apr 27, 2:29 am, Josselin <josse...@wanadoo.fr> 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"
Whoa. I hadn't seen that syntax before. Nice.
--
Alex
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 2007-04-27 09:36:31 +0200, Stefano Crocco <stefano.crocco@alice.it> said:
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 theoriginal one, use sub! instead of sub).
I hope this helps
Stefano
thanks Dan, but I looked for more generic replacement.. as Stefano mentionned
joss
On 2007-04-27 09:39:04 +0200, Dan Zwell <dzwell@gmail.com> said:
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 ?
tfyh
joss
str.gsub!(/31551/, "9999")
good luck,
dan
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 09:52:29 +0200, William James <w_a_x_man@yahoo.com> said:
On Apr 27, 2:29 am, Josselin <josse...@wanadoo.fr> 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"
I forgot to mention my main objective > replacing the term after the last '/'..
thanks to all
Joss
On 2007-04-27 11:15:15 +0200, Robert Klemme <shortcutter@googlemail.com> said:
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.
Kind regards
robert
<snip>
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
On 4/27/07, Josselin <josselin@wanadoo.fr> wrote:
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"
=> niljosss :-))
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw
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 2007-04-27 10:01:07 +0200, Alex Young <alex@blackkettle.org> said:
William James wrote:
On Apr 27, 2:29 am, Josselin <josse...@wanadoo.fr> 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"
Whoa. I hadn't seen that syntax before. Nice.
In that case I'd use the File approach or do str[%r{[^/]+$}]="9999"
robert
On 27.04.2007 15:18, Josselin wrote:
On 2007-04-27 11:15:15 +0200, Robert Klemme <shortcutter@googlemail.com> > said:
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.
Kind regards
robert
I forgot to mention my main objective > replacing the term after the last '/'..
thanks to all
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.
--
Ist so, weil ist so
Bleibt so, weil war so