[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Replacing ' with \' using gsub

Jani Soila

7/21/2006 5:04:00 PM

I tried to replace "'" characters with "\'" strings using gsub. What
happened instead was something completely different. Am I missing
something here?
(in irb)
irb(main):001:0> s = "a'b'"
=> "a'b'"
Unexpected result
irb(main):002:0> s.gsub(/'/, "\\'")
=> "ab'b"
Also with this
irb(main):003:0> s.gsub(/'/, '\\\'')
=> "ab'b"
However this works
irb(main):004:0> s.gsub(/'/, "\\x'")
=> "a\\x'b\\x'"

Thank you for your help!
-js

--
Posted via http://www.ruby-....

5 Answers

Jano Svitok

7/21/2006 5:18:00 PM

0

Seems to me like a bug in the regex code.
using ruby 1.8.4 (2005-12-24) [i386-mswin32]

J.

On 7/21/06, Jani Soila <janisoila@gmail.com> wrote:
> I tried to replace "'" characters with "\'" strings using gsub. What
> happened instead was something completely different. Am I missing
> something here?
> (in irb)
> irb(main):001:0> s = "a'b'"
> => "a'b'"
> Unexpected result
> irb(main):002:0> s.gsub(/'/, "\\'")
> => "ab'b"
> Also with this
> irb(main):003:0> s.gsub(/'/, '\\\'')
> => "ab'b"
> However this works
> irb(main):004:0> s.gsub(/'/, "\\x'")
> => "a\\x'b\\x'"
>
> Thank you for your help!
> -js
>
> --
> Posted via http://www.ruby-....
>
>

Gavin Kistner

7/21/2006 5:22:00 PM

0

Jan Svitok wrote:
> Seems to me like a bug in the regex code.
> using ruby 1.8.4 (2005-12-24) [i386-mswin32]

But it isn't. You need more backslashes.

See "Explanation for the multitude of backslashes " at
http://wiki.rubygarden.org/Ruby/page/show/Rege...
or search the list archives, as this comes up frequently by newcomers
(including myself, when I was young ;).

Morton Goldberg

7/21/2006 5:28:00 PM

0

irb(main):001:0> s = "a'b'"
=> "a'b'"
irb(main):002:0> s.gsub(/'/, "\\\'")
=> "ab'b"
irb(main):003:0> s.gsub(/'/, "\\\\\'")
=> "a\\'b\\'"

Regards, Morton

On Jul 21, 2006, at 1:04 PM, Jani Soila wrote:

> I tried to replace "'" characters with "\'" strings using gsub. What
> happened instead was something completely different. Am I missing
> something here?
> (in irb)
> irb(main):001:0> s = "a'b'"
> => "a'b'"
> Unexpected result
> irb(main):002:0> s.gsub(/'/, "\\'")
> => "ab'b"
> Also with this
> irb(main):003:0> s.gsub(/'/, '\\\'')
> => "ab'b"
> However this works
> irb(main):004:0> s.gsub(/'/, "\\x'")
> => "a\\x'b\\x'"
>
> Thank you for your help!
> -js
>
> --
> Posted via http://www.ruby-....
>


ts

7/21/2006 5:30:00 PM

0

>>>>> "J" == Jani Soila <janisoila@gmail.com> writes:

J> irb(main):001:0> s = "a'b'"
J> => "a'b'"
J> Unexpected result
J> irb(main):002:0> s.gsub(/'/, "\\'")
J> => "ab'b"

"\\'" is the same than $', i.e. MatchData#post_match. For example

irb(main):001:0> s = "a'b'"
=> "a'b'"
irb(main):002:0> s.match(/'/).post_match
=> "b'"
irb(main):003:0>

ruby replace the first <'> with <b'>
the second <'> with <> (there is nothing after the last ')


Guy Decoux

Logan Capaldo

7/21/2006 5:38:00 PM

0


On Jul 21, 2006, at 1:30 PM, ts wrote:

>>>>>> "J" == Jani Soila <janisoila@gmail.com> writes:
>
> J> irb(main):001:0> s = "a'b'"
> J> => "a'b'"
> J> Unexpected result
> J> irb(main):002:0> s.gsub(/'/, "\\'")
> J> => "ab'b"
>
> "\\'" is the same than $', i.e. MatchData#post_match. For example
>
> irb(main):001:0> s = "a'b'"
> => "a'b'"
> irb(main):002:0> s.match(/'/).post_match
> => "b'"
> irb(main):003:0>
>
> ruby replace the first <'> with <b'>
> the second <'> with <> (there is nothing after the
> last ')
>
>
> Guy Decoux
>

What he said.

Also if you don't want to drive yourself crazy with fifteen billion
backslahes:

str.gsub(/'/) { %q{\'} }