string replacement...

Discussion in 'Ruby' started by Josselin, Apr 27, 2007.

  1. Josselin

    Josselin Guest

    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
     
    Josselin, Apr 27, 2007
    #1
    1. Advertising

  2. 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
     
    Stefano Crocco, Apr 27, 2007
    #2
    1. Advertising

  3. 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"

    :)
     
    Björn Paetzel, Apr 27, 2007
    #3
  4. Josselin

    Dan Zwell Guest

    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
     
    Dan Zwell, Apr 27, 2007
    #4
  5. On Apr 27, 2:29 am, 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[/\d+/] = "9999"
     
    William James, Apr 27, 2007
    #5
  6. Josselin

    Alex Young Guest

    William James wrote:
    > On Apr 27, 2:29 am, 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[/\d+/] = "9999"

    Whoa. I hadn't seen that syntax before. Nice.

    --
    Alex
     
    Alex Young, Apr 27, 2007
    #6
  7. 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
     
    Robert Klemme, Apr 27, 2007
    #7
  8. Josselin

    Josselin Guest

    On 2007-04-27 09:36:31 +0200, Stefano Crocco <> 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 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"

    ;-)))
     
    Josselin, Apr 27, 2007
    #8
  9. Josselin

    Josselin Guest

    On 2007-04-27 09:37:25 +0200, Björn Paetzel <> 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"
    >
    > :)


    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 :))
     
    Josselin, Apr 27, 2007
    #9
  10. Josselin

    Josselin Guest

    On 2007-04-27 09:39:04 +0200, Dan Zwell <> 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


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

    joss
     
    Josselin, Apr 27, 2007
    #10
  11. Josselin

    Josselin Guest

    On 2007-04-27 09:52:29 +0200, William James <> said:

    > On Apr 27, 2:29 am, 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[/\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
     
    Josselin, Apr 27, 2007
    #11
  12. Josselin

    Josselin Guest

    On 2007-04-27 10:01:07 +0200, Alex Young <> said:

    > William James wrote:
    >> On Apr 27, 2:29 am, 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[/\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
     
    Josselin, Apr 27, 2007
    #12
  13. Josselin

    Josselin Guest

    On 2007-04-27 11:15:15 +0200, Robert Klemme <> 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

    Joss
     
    Josselin, Apr 27, 2007
    #13
  14. On 27.04.2007 15:18, Josselin wrote:
    > On 2007-04-27 11:15:15 +0200, Robert Klemme <>
    > 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


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

    robert
     
    Robert Klemme, Apr 27, 2007
    #14
  15. 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
     
    Sebastian Hungerecker, Apr 27, 2007
    #15
  16. Josselin

    Robert Dober Guest

    On 4/27/07, Josselin <> wrote:
    <snip>
    >
    > 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
     
    Robert Dober, Apr 27, 2007
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mladen Adamovic
    Replies:
    0
    Views:
    744
    Mladen Adamovic
    Dec 4, 2003
  2. Mladen Adamovic
    Replies:
    3
    Views:
    14,637
    Mladen Adamovic
    Dec 5, 2003
  3. Chris Nevill

    Regex String Replacement

    Chris Nevill, Jan 31, 2004, in forum: Java
    Replies:
    5
    Views:
    477
    Chris Nevill
    Jan 31, 2004
  4. qazmlp
    Replies:
    5
    Views:
    694
    Michael Dunn
    Apr 7, 2004
  5. ridcully
    Replies:
    4
    Views:
    126
Loading...

Share This Page