regex to escape special characters

Discussion in 'Ruby' started by Jon Garvin, Feb 10, 2009.

  1. Jon Garvin

    Jon Garvin Guest

    I've got the following string...

    "This is ( a test"

    And I want to do a regex gsub on it to turn it into...

    "This is \( a test"

    In irb, If I start with...

    "This is ( a test".gsub(/[\(]/,'\&') => "This is ( a test"

    And then progressively added more backslashes, I get the following
    results, none of which are what I'm looking for. The second one is,
    IMO, the one that should work.

    "This is ( a test".gsub(/[\(]/,'\\&') => "This is ( a test"
    "This is ( a test".gsub(/[\(]/,'\\\&') => "This is \\& a test"
    "This is ( a test".gsub(/[\(]/,'\\\\&') => "This is \\& a test"
    "This is ( a test".gsub(/[\(]/,'\\\\\&')=> "This is \\( a test"


    Am I just having a serious case of the mondays, or is there a bug in how
    Ruby deals with \ characters when trying to represent an actual single \
    character?

    I'm using ruby 1.8.6 (2007-09-24 patchlevel 111) [i486-linux]

    --

    http://www.5valleys.com/

    http://www.workingwithrails.com/person/8078
     
    Jon Garvin, Feb 10, 2009
    #1
    1. Advertising

  2. Jon Garvin

    Tim Hunter Guest

    Jon Garvin wrote:
    > I've got the following string...
    >
    > "This is ( a test"
    >
    > And I want to do a regex gsub on it to turn it into...
    >
    > "This is \( a test"
    >
    > In irb, If I start with...
    >
    > "This is ( a test".gsub(/[\(]/,'\&') => "This is ( a test"
    >
    > And then progressively added more backslashes, I get the following
    > results, none of which are what I'm looking for. The second one is,
    > IMO, the one that should work.
    >
    > "This is ( a test".gsub(/[\(]/,'\\&') => "This is ( a test"
    > "This is ( a test".gsub(/[\(]/,'\\\&') => "This is \\& a test"
    > "This is ( a test".gsub(/[\(]/,'\\\\&') => "This is \\& a test"
    > "This is ( a test".gsub(/[\(]/,'\\\\\&')=> "This is \\( a test"
    >
    >
    > Am I just having a serious case of the mondays, or is there a bug in how
    > Ruby deals with \ characters when trying to represent an actual single \
    > character?
    >
    > I'm using ruby 1.8.6 (2007-09-24 patchlevel 111) [i486-linux]


    irb shows you the result using p, which escapes special characters in
    the string it displays. Use puts to see the actual string.

    C:\>irb
    irb(main):001:0> x = "This is ( a test"
    => "This is ( a test"
    irb(main):002:0> x.gsub(/\(/, '\\\(')
    => "This is \\( a test"
    irb(main):003:0> y = x.gsub(/\(/, '\\\(')
    => "This is \\( a test"
    irb(main):004:0> puts y
    This is \( a test
    => nil
    irb(main):005:0>
    --
    Posted via http://www.ruby-forum.com/.
     
    Tim Hunter, Feb 10, 2009
    #2
    1. Advertising

  3. Jon Garvin

    Jon Garvin Guest

    [Note: parts of this message were removed to make it a legal post.]

    Tim Hunter wrote:
    > Jon Garvin wrote:
    >
    >> I've got the following string...
    >>
    >> "This is ( a test"
    >>
    >> And I want to do a regex gsub on it to turn it into...
    >>
    >> "This is \( a test"
    >>
    >> In irb, If I start with...
    >>
    >> "This is ( a test".gsub(/[\(]/,'\&') => "This is ( a test"
    >>
    >> And then progressively added more backslashes, I get the following
    >> results, none of which are what I'm looking for. The second one is,
    >> IMO, the one that should work.
    >>
    >> "This is ( a test".gsub(/[\(]/,'\\&') => "This is ( a test"
    >> "This is ( a test".gsub(/[\(]/,'\\\&') => "This is \\& a test"
    >> "This is ( a test".gsub(/[\(]/,'\\\\&') => "This is \\& a test"
    >> "This is ( a test".gsub(/[\(]/,'\\\\\&')=> "This is \\( a test"
    >>
    >>
    >> Am I just having a serious case of the mondays, or is there a bug in how
    >> Ruby deals with \ characters when trying to represent an actual single \
    >> character?
    >>
    >> I'm using ruby 1.8.6 (2007-09-24 patchlevel 111) [i486-linux]
    >>

    >
    > irb shows you the result using p, which escapes special characters in
    > the string it displays. Use puts to see the actual string.
    >
    > C:\>irb
    > irb(main):001:0> x = "This is ( a test"
    > => "This is ( a test"
    > irb(main):002:0> x.gsub(/\(/, '\\\(')
    > => "This is \\( a test"
    > irb(main):003:0> y = x.gsub(/\(/, '\\\(')
    > => "This is \\( a test"
    > irb(main):004:0> puts y
    > This is \( a test
    > => nil
    > irb(main):005:0>
    >

    Oh, fer cryin' out loud. Case of the mondays it is. Thanks.

    --

    http://www.5valleys.com/

    http://www.workingwithrails.com/person/8078
     
    Jon Garvin, Feb 10, 2009
    #3
  4. Jon Garvin

    badboy Guest

    Jon Garvin schrieb:
    > I've got the following string...
    >
    > "This is ( a test"
    >
    > And I want to do a regex gsub on it to turn it into...
    >
    > "This is \( a test"
    >
    > In irb, If I start with...
    >
    > "This is ( a test".gsub(/[\(]/,'\&') => "This is ( a test"
    >
    > And then progressively added more backslashes, I get the following
    > results, none of which are what I'm looking for. The second one is,
    > IMO, the one that should work.
    >
    > "This is ( a test".gsub(/[\(]/,'\\&') => "This is ( a test"
    > "This is ( a test".gsub(/[\(]/,'\\\&') => "This is \\& a test"
    > "This is ( a test".gsub(/[\(]/,'\\\\&') => "This is \\& a test"
    > "This is ( a test".gsub(/[\(]/,'\\\\\&')=> "This is \\( a test"
    >
    >
    > Am I just having a serious case of the mondays, or is there a bug in how
    > Ruby deals with \ characters when trying to represent an actual single \
    > character?
    >
    > I'm using ruby 1.8.6 (2007-09-24 patchlevel 111) [i486-linux]
    >

    you know Regexp.escape ?
     
    badboy, Feb 10, 2009
    #4
  5. Jon Garvin

    Tom Cloyd Guest

    badboy wrote:
    > Jon Garvin schrieb:
    >
    >> I've got the following string...
    >>
    >> "This is ( a test"
    >>
    >> And I want to do a regex gsub on it to turn it into...
    >>
    >> "This is \( a test"
    >>
    >> In irb, If I start with...
    >>
    >> "This is ( a test".gsub(/[\(]/,'\&') => "This is ( a test"
    >>
    >> And then progressively added more backslashes, I get the following
    >> results, none of which are what I'm looking for. The second one is,
    >> IMO, the one that should work.
    >>
    >> "This is ( a test".gsub(/[\(]/,'\\&') => "This is ( a test"
    >> "This is ( a test".gsub(/[\(]/,'\\\&') => "This is \\& a test"
    >> "This is ( a test".gsub(/[\(]/,'\\\\&') => "This is \\& a test"
    >> "This is ( a test".gsub(/[\(]/,'\\\\\&')=> "This is \\( a test"
    >>
    >>
    >> Am I just having a serious case of the mondays, or is there a bug in how
    >> Ruby deals with \ characters when trying to represent an actual single \
    >> character?
    >>
    >> I'm using ruby 1.8.6 (2007-09-24 patchlevel 111) [i486-linux]
    >>
    >>

    > you know Regexp.escape ?
    >
    >
    >

    Yikes...thanks for the reminder. I always seem to find 6 hard ways to do
    something before learning (and occasionally discovering) an easy way.
    Regexp.escape solve LOTS of problems quickly. ~t.

    --

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
    Bellingham, Washington, U.S.A: (360) 920-1226
    << >> (email)
    << TomCloyd.com >> (website)
    << sleightmind.wordpress.com >> (mental health weblog)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     
    Tom Cloyd, Feb 11, 2009
    #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. Stefan Mueller
    Replies:
    3
    Views:
    33,312
    Stefan Mueller
    Jul 23, 2006
  2. Replies:
    2
    Views:
    1,144
    Ingo Menger
    May 31, 2007
  3. rvino
    Replies:
    0
    Views:
    4,721
    rvino
    Aug 14, 2007
  4. majna
    Replies:
    4
    Views:
    784
    Thomas 'PointedEars' Lahn
    Sep 19, 2007
  5. Gabriela
    Replies:
    4
    Views:
    583
    Tim Greer
    Dec 22, 2008
Loading...

Share This Page