Problem with String.gsub and \' as a replacement string

Discussion in 'Ruby' started by ridcully, Nov 17, 2007.

  1. ridcully

    ridcully Guest

    Hi,

    I want to replace all occurences of a certain character in a string
    with a backslash followed by a single quote. Sounds like a trivial
    task, but this is what I get:

    "this is a test".gsub( "a", "\\'" ) -> "this is test test"

    What I want is "this is \' test".

    Neither does this work:
    "this is a test".gsub( "a", '\' + "'" )

    No matter what I am doing, as soon as a backslash is followed by a
    single quote in the replacement string, I am getting weird results.

    Thanks for your help!

    Andreas
    ridcully, Nov 17, 2007
    #1
    1. Advertising

  2. ridcully

    Alex Young Guest

    ridcully wrote:
    > Hi,
    >
    > I want to replace all occurences of a certain character in a string
    > with a backslash followed by a single quote. Sounds like a trivial
    > task, but this is what I get:
    >
    > "this is a test".gsub( "a", "\\'" ) -> "this is test test"
    >
    > What I want is "this is \' test".
    >
    > Neither does this work:
    > "this is a test".gsub( "a", '\' + "'" )
    >
    > No matter what I am doing, as soon as a backslash is followed by a
    > single quote in the replacement string, I am getting weird results.

    Backslashes are confusing. Try this:

    puts "this is a test".gsub( "a", "\\\\'" )

    --
    Alex
    Alex Young, Nov 17, 2007
    #2
    1. Advertising

  3. ridcully

    ridcully Guest

    On 17 Nov., 12:22, Alex Young <> wrote:
    > Backslashes are confusing. Try this:
    >
    > puts "this is a test".gsub( "a", "\\\\'" )
    >
    > --
    > Alex


    Thank you, you saved my day!

    This really is confusing, because "\\'" gives me the correct result if
    I don't use it with gsub:

    puts "\\'" -> \'

    Am I missing something here?

    Andreas
    ridcully, Nov 17, 2007
    #3
  4. ridcully wrote:
    > On 17 Nov., 12:22, Alex Young <> wrote:
    > > puts "this is a test".gsub( "a", "\\\\'" )

    >
    > Thank you, you saved my day!
    >
    > This really is confusing, because "\\'" gives me the correct result if
    > I don't use it with gsub:
    >
    > puts "\\'" -> \'
    >
    > Am I missing something here?


    "\\'" translates to a literal backslash followed by '. This is what gsub gets.
    gsub then sees \' and replaces it with $' the same way it would replace \1
    with $1. To tell it not to do that it has to get \\', so it knows it's not
    supposed to treat \' as special. In order to archieve that you have to
    write "\\\\'". Hope that cleared things up for you.


    --
    NP: Falkenbach - Vanadis
    Jabber:
    ICQ: 205544826
    Sebastian Hungerecker, Nov 17, 2007
    #4
  5. ridcully

    botp Guest

    On Nov 17, 2007 7:20 PM, ridcully <> wrote:
    > "this is a test".gsub( "a", "\\'" ) -> "this is test test"


    many ways, eg

    ~> "this is a test".gsub("i",Regexp.escape("\\'"))
    => "th\\'s \\'s a test"

    ~> "this is a test".gsub("i"){"\\'"}
    => "th\\'s \\'s a test"

    the block form seems clean though.

    kind regards -botp
    botp, Nov 17, 2007
    #5
    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:
    724
    Mladen Adamovic
    Dec 4, 2003
  2. James R. Leu

    gsub with match replacement

    James R. Leu, Feb 3, 2004, in forum: Ruby
    Replies:
    1
    Views:
    115
    Carlos
    Feb 3, 2004
  3. aurelianito

    gsub and gsub! are inconsistent

    aurelianito, Nov 8, 2005, in forum: Ruby
    Replies:
    9
    Views:
    154
    Robert Klemme
    Nov 9, 2005
  4. Shea Barton

    Partial GSUB match / replacement

    Shea Barton, Nov 20, 2010, in forum: Ruby
    Replies:
    6
    Views:
    113
    Shea Barton
    Nov 21, 2010
  5. Greg Hacke

    gsub and multiple-replacement

    Greg Hacke, May 10, 2011, in forum: Ruby
    Replies:
    10
    Views:
    676
    7stud --
    May 12, 2011
Loading...

Share This Page