# Replacing ' with \\' using gsub

**URL:** <https://www.ruby-forum.com/t/replacing-with-using-gsub/99325>\
**Category:** Ruby\
**Created:** [July 21, 2006, 7:04pm UTC](https://www.ruby-forum.com/t/replacing-with-using-gsub/99325 "2006-07-21T19:04:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![soila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@soila](https://www.ruby-forum.com/u/soila)\
**Post date:** [July 21, 2006, 7:04pm UTC](https://www.ruby-forum.com/t/replacing-with-using-gsub/99325/1 "2006-07-21T19:04:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![soila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@soila](https://www.ruby-forum.com/u/soila)\
**Post date:** [July 21, 2006, 7:20pm UTC](https://www.ruby-forum.com/t/replacing-with-using-gsub/99325/2 "2006-07-21T19:20:54Z")

</div>

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

J.

---

<div class="post-metadata">

**Author:** ![soila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@soila](https://www.ruby-forum.com/u/soila)\
**Post date:** [July 21, 2006, 7:27pm UTC](https://www.ruby-forum.com/t/replacing-with-using-gsub/99325/3 "2006-07-21T19:27:16Z")

</div>

Jan S. 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/RegexpCookbook](http://wiki.rubygarden.org/Ruby/page/show/RegexpCookbook)  
or search the list archives, as this comes up frequently by newcomers  
(including myself, when I was young ;).

---

<div class="post-metadata">

**Author:** ![soila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@soila](https://www.ruby-forum.com/u/soila)\
**Post date:** [July 21, 2006, 7:30pm UTC](https://www.ruby-forum.com/t/replacing-with-using-gsub/99325/4 "2006-07-21T19:30:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![soila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@soila](https://www.ruby-forum.com/u/soila)\
**Post date:** [July 21, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/replacing-with-using-gsub/99325/5 "2006-07-21T19:34:07Z")

</div>

> > > > > “J” == Jani S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![soila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@soila](https://www.ruby-forum.com/u/soila)\
**Post date:** [July 21, 2006, 8:59pm UTC](https://www.ruby-forum.com/t/replacing-with-using-gsub/99325/6 "2006-07-21T20:59:09Z")

</div>

On 7/21/06, ts [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> “\'” is the same than $', i.e. MatchData#post\_match. For example

Now that makes more sense… I didn’t know that. After looking in the  
documentation, I found this only in PickAxe book. I haven’t found it  
neither in the 1.8 RDoc nor in the mentioned article.

Attached is a patch against 1.182.2.48 revision of string.c, that adds  
these sequences to rdoc (shamelessly ripped from the pickaxe book).

J.

---

<div class="post-metadata">

**Author:** ![soila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@soila](https://www.ruby-forum.com/u/soila)\
**Post date:** [July 21, 2006, 7:40pm UTC](https://www.ruby-forum.com/t/replacing-with-using-gsub/99325/7 "2006-07-21T19:40:39Z")

</div>

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

> irb(main):001:0\> s = “a’b’”  
> Guy Decoux

What he said.

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

str.gsub(/’/) { %q{’} }
