Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > regex to escape special characters

Reply
Thread Tools

regex to escape special characters

 
 
Jon Garvin
Guest
Posts: n/a
 
      02-10-2009
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


 
Reply With Quote
 
 
 
 
Tim Hunter
Guest
Posts: n/a
 
      02-10-2009
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/.

 
Reply With Quote
 
 
 
 
Jon Garvin
Guest
Posts: n/a
 
      02-10-2009
[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


 
Reply With Quote
 
badboy
Guest
Posts: n/a
 
      02-10-2009
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 ?

 
Reply With Quote
 
Tom Cloyd
Guest
Posts: n/a
 
      02-11-2009
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
<< http://www.velocityreviews.com/forums/(E-Mail Removed) >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RegExp pattern to escape ALL special characters (but exclude unicodechars) Gabriela Javascript 4 12-22-2008 06:04 PM
Remove only special characters and junk characters from a file rvino Perl 0 08-14-2007 07:23 AM
Re: Meta-Characters, Special Characters xah@xahlee.org Java 2 05-31-2007 09:25 AM
How to convert HTML special characters to the real characters with a Java script Stefan Mueller HTML 3 07-23-2006 10:09 PM
Re: html special character and escape characters knowledgepays@hotmail.com ASP .Net 0 01-27-2005 02:08 AM



Advertisments