[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

regex to escape special characters

Jon Garvin

2/10/2009 6:12:00 PM

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.5va...

http://www.workingwithrails.com/p...


6 Answers

Tim Hunter

2/10/2009 6:22:00 PM

0

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

Jon Garvin

2/10/2009 6:27:00 PM

0

[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.5va...

http://www.workingwithrails.com/p...


Jan-Erik R.

2/10/2009 6:48:00 PM

0

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 ?

Tom Cloyd

2/11/2009 4:44:00 AM

0

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
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Borg

4/25/2010 3:27:00 PM

0

BBC
London
WC1 IDK


Dear Jim. Can you fix it for these people to get a life ?

Thanks

Borg Aged 12 3/4


There will that do ?

US 71

4/27/2010 12:40:00 PM

0



>I demand that the BBC publicly guarantees that it will never do anything
>like this again.
>

Looks like you got your wish. :o)