Forum: Ruby regex to escape special characters

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3299251223f55753ec84719de39e6e53?d=identicon&s=25 Jon Garvin (Guest)
on 2009-02-10 19:14
(Received via mailing list)
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
B33ea5c12d767bfd1253940a960274f5?d=identicon&s=25 Tim Hunter (timhunter)
on 2009-02-10 19:23
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>
3299251223f55753ec84719de39e6e53?d=identicon&s=25 Jon Garvin (Guest)
on 2009-02-10 19:28
(Received via mailing list)
Tim Hunter wrote:
>> In irb, If I start with...
>> "This is ( a test".gsub(/[\(]/,'\\\\\&')=> "This is \\( a test"
> the string it displays. Use puts to see the actual string.
> => 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
245a6d22816ecaeac1c9080ad183b859?d=identicon&s=25 badboy (Guest)
on 2009-02-10 19:50
(Received via mailing list)
Jon Garvin schrieb:
>  "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 ?
D15a45a973443d4562051eb675b60474?d=identicon&s=25 Tom Cloyd (Guest)
on 2009-02-11 05:49
(Received via mailing list)
badboy wrote:
>> In irb, If I start with...
>> "This is ( a test".gsub(/[\(]/,'\\\\\&')=> "This is \\( a test"
>
>
>
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
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This topic is locked and can not be replied to.