# Gsub("\\\\", "\\\\\\\\") seems unintuitive

**URL:** <https://rubytalk.org/t/gsub-seems-unintuitive/44701>\
**Category:** ruby-talk\
**Created:** [22 February 2008 17:27 UTC](https://rubytalk.org/t/gsub-seems-unintuitive/44701 "2008-02-22T17:27:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Woods](https://avatars.discourse-cdn.com/v4/letter/j/a88e4f/32.png) [@John\_Woods](https://rubytalk.org/u/John_Woods)\
**Post date:** [22 February 2008 17:27 UTC](https://rubytalk.org/t/gsub-seems-unintuitive/44701/1 "2008-02-22T17:27:44Z")

</div>

The following confusing behavior is noted in the pickaxe book (2nd ed) on page 75:

&nbsp;&nbsp;&nbsp;# I would expect two backslashes in the result  
&nbsp;&nbsp;&nbsp;\> puts "\\".gsub("\\","\\\\")  
&nbsp;&nbsp;&nbsp;\

&nbsp;&nbsp;&nbsp;# I would expect four backslashes in the result  
&nbsp;&nbsp;&nbsp;\> puts "\\".gsub("\\","\\\\\\\\")  
&nbsp;&nbsp;&nbsp;\\

I can certainly work around it, but it seems unintuitive. Is there a reason why gsub behaves this way? Just curious...

---

<div class="post-metadata">

**Author:** ![J\_Cooper](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@J\_Cooper](https://rubytalk.org/u/J_Cooper)\
**Post date:** [22 February 2008 17:37 UTC](https://rubytalk.org/t/gsub-seems-unintuitive/44701/2 "2008-02-22T17:37:49Z")

</div>

John Woods wrote:

> The following confusing behavior is noted in the pickaxe book (2nd ed)  
> on page 75:
> 
> &nbsp;&nbsp;&nbsp;# I would expect two backslashes in the result  
> &nbsp;&nbsp;&nbsp;\> puts "\\".gsub("\\","\\\\")  
> &nbsp;&nbsp;&nbsp;\
> 
> &nbsp;&nbsp;&nbsp;# I would expect four backslashes in the result  
> &nbsp;&nbsp;&nbsp;\> puts "\\".gsub("\\","\\\\\\\\")  
> &nbsp;&nbsp;&nbsp;\\
> 
> I can certainly work around it, but it seems unintuitive. Is there a  
> reason why gsub behaves this way? Just curious...

It's not a gsub thing, per se--it's a string thing. Backslashes are used  
in strings to escape special characters. One such character is a " mark.  
If you want to write a " mark in the middle of a string, you have to  
escape it with a backslash:  
"They call me \"Mellow Yellow\" etc."

If you didn't, then the " would signify the end of the string!  
Similarly, in the example you listed, if you just did:  
"\"  
then you end up with a string that ISN'T ended! Because you escaped the  
next ". So, if you want a literal backslash, you have to escape the  
backslash too: "\\"

It just looks confusing because you are escaping the escape character 🙂

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [22 February 2008 17:40 UTC](https://rubytalk.org/t/gsub-seems-unintuitive/44701/3 "2008-02-22T17:40:00Z")

</div>

puts "\\".gsub("\\"){"\\\\"}

> **···**
>
> On Feb 22, 11:27 am, John Woods \<jqwo...@gmail.com\> wrote:
> 
> > The following confusing behavior is noted in the pickaxe book (2nd ed)  
> > on page 75:
> > 
> > &nbsp;&nbsp;&nbsp;# I would expect two backslashes in the result  
> > &nbsp;&nbsp;&nbsp;\> puts "\\".gsub("\\","\\\\")  
> > &nbsp;&nbsp;&nbsp;\
> > 
> > &nbsp;&nbsp;&nbsp;# I would expect four backslashes in the result  
> > &nbsp;&nbsp;&nbsp;\> puts "\\".gsub("\\","\\\\\\\\")  
> > &nbsp;&nbsp;&nbsp;\\
> > 
> > I can certainly work around it, but it seems unintuitive. Is there a  
> > reason why gsub behaves this way? Just curious...

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [22 February 2008 17:48 UTC](https://rubytalk.org/t/gsub-seems-unintuitive/44701/4 "2008-02-22T17:48:44Z")

</div>

Notwithstanding the earlier responses...

Since the replacement string is evaluated 'twice', once as a ruby string literal and then again by gsub to look for group refrences like '\1', you need to provide two levels of escaping for a backslash.

\ is "\\"  
so two of them is "\\\\"  
and you want gsub to see that so it need to have them escaped: "\\\\\\\\"

Whew! Yeah, it's unfortunate, but backslash is doing double-duty here: introducing a group reference to the regular expression and escaping characters in a string literal (just like "\n", but also itself).

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
Rob@AgileConsultingLLC.com

> **···**
>
> On Feb 22, 2008, at 12:27 PM, John Woods wrote:
> 
> > The following confusing behavior is noted in the pickaxe book (2nd ed) on page 75:
> > 
> > # I would expect two backslashes in the result  
> > \> puts "\\".gsub("\\","\\\\")  
> > \
> > 
> > # I would expect four backslashes in the result  
> > \> puts "\\".gsub("\\","\\\\\\\\")  
> > \\
> > 
> > I can certainly work around it, but it seems unintuitive. Is there a reason why gsub behaves this way? Just curious...

---

<div class="post-metadata">

**Author:** ![Kyle\_Schmitt](https://avatars.discourse-cdn.com/v4/letter/k/9e8a1a/32.png) [@Kyle\_Schmitt](https://rubytalk.org/u/Kyle_Schmitt)\
**Post date:** [22 February 2008 17:53 UTC](https://rubytalk.org/t/gsub-seems-unintuitive/44701/5 "2008-02-22T17:53:38Z")

</div>

Just as a side note, this is typical in all real programming languages.  
C, C++, Java, Perl, sh, etc.  
I \_believe\_ it's also true in python, lithp/scheme, & (o)caml, but for  
those I've either not used them, or not used them in so long I'm  
unsure.

Some languages, like vb{[6|script|.net](http://6%7Cscript%7C.net)}, use a doubled quote, but those  
aren't really proper programing languages 😉

--Kyle

---

<div class="post-metadata">

**Author:** ![John\_Woods](https://avatars.discourse-cdn.com/v4/letter/j/a88e4f/32.png) [@John\_Woods](https://rubytalk.org/u/John_Woods)\
**Post date:** [22 February 2008 18:01 UTC](https://rubytalk.org/t/gsub-seems-unintuitive/44701/6 "2008-02-22T18:01:08Z")

</div>

Thanks Rob, that's exactly what I was missing -- the second round of escaping is necessary to make escaped references to regex groups work.

> **···**
>
> -----Original Message-----  
> From: Rob Biedenharn  
> Sent: 02/22/2008 09:48 AM
> 
> > On Feb 22, 2008, at 12:27 PM, John Woods wrote:
> > 
> > > The following confusing behavior is noted in the pickaxe book (2nd ed) on page 75:
> > > 
> > > # I would expect two backslashes in the result  
> > > \> puts "\\".gsub("\\","\\\\")  
> > > \
> > > 
> > > # I would expect four backslashes in the result  
> > > \> puts "\\".gsub("\\","\\\\\\\\")  
> > > \\
> > > 
> > > I can certainly work around it, but it seems unintuitive. Is there a reason why gsub behaves this way? Just curious...
> > 
> > Notwithstanding the earlier responses...
> > 
> > Since the replacement string is evaluated 'twice', once as a ruby string literal and then again by gsub to look for group refrences like '\1', you need to provide two levels of escaping for a backslash.
> > 
> > \ is "\\"  
> > so two of them is "\\\\"  
> > and you want gsub to see that so it need to have them escaped: "\\\\\\\\"
> > 
> > Whew! Yeah, it's unfortunate, but backslash is doing double-duty here: introducing a group reference to the regular expression and escaping characters in a string literal (just like "\n", but also itself).
> > 
> > -Rob
> > 
> > Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> > Rob@AgileConsultingLLC.com

---

<div class="post-metadata">

**Author:** ![Joe\_Peck](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@Joe\_Peck](https://rubytalk.org/u/Joe_Peck)\
**Post date:** [17 December 2008 21:07 UTC](https://rubytalk.org/t/gsub-seems-unintuitive/44701/7 "2008-12-17T21:07:51Z")

</div>

I stumbled onto this thread trying to figure out a similar problem...

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Joe\_Peck](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@Joe\_Peck](https://rubytalk.org/u/Joe_Peck)\
**Post date:** [17 December 2008 21:12 UTC](https://rubytalk.org/t/gsub-seems-unintuitive/44701/8 "2008-12-17T21:12:03Z")

</div>

Joe Peck wrote:

> I stumbled onto this thread trying to figure out a similar problem...

Dangit, I didn't mean to post the above message 🙂

Okay, I need to read a site name (like "joe.com") and put a \ in front  
of the period (so it would be "joe\.com"). I'm having trouble with  
this, I can't seem to get only ONE \ in front of the period...

> > "joe.com".gsub(/\./, "\\\.")

=\> "joe\\.com"

> > "joe.com".gsub(/\./, "\\\\.")

=\> "joe\\.com"

Anyone have a clue what to do? I'm probably missing something simple.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Glen\_Holcomb](https://avatars.discourse-cdn.com/v4/letter/g/ecc23a/32.png) [@Glen\_Holcomb](https://rubytalk.org/u/Glen_Holcomb)\
**Post date:** [17 December 2008 21:20 UTC](https://rubytalk.org/t/gsub-seems-unintuitive/44701/9 "2008-12-17T21:20:47Z")

</div>

That's because you are in irb. You are getting what you want try this:

puts "joe.com".gsub(/\./, "\\\\.")

Actually it might be more readable as gsub('.', '\.')

> **···**
>
> On Wed, Dec 17, 2008 at 2:12 PM, Joe Peck \<joe@notsleepy.com\> wrote:
> 
> > Joe Peck wrote:  
> > \> I stumbled onto this thread trying to figure out a similar problem...
> > 
> > Dangit, I didn't mean to post the above message 🙂
> > 
> > Okay, I need to read a site name (like "joe.com") and put a \ in front  
> > of the period (so it would be "joe\.com"). I'm having trouble with  
> > this, I can't seem to get only ONE \ in front of the period...
> > 
> > \>\> "joe.com".gsub(/\./, "\\\.")  
> > =\> "joe\\.com"  
> > \>\> "joe.com".gsub(/\./, "\\\\.")  
> > =\> "joe\\.com"
> > 
> > Anyone have a clue what to do? I'm probably missing something simple.  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> "Hey brother Christian with your high and mighty errand, Your actions speak  
> so loud, I can't hear a word you're saying."
> 
> -Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [17 December 2008 22:56 UTC](https://rubytalk.org/t/gsub-seems-unintuitive/44701/10 "2008-12-17T22:56:24Z")

</div>

You are looking at a string with escaping characters (i.e. what it  
would look like in double quotes). You can verify this by using #puts  
or "joe.com".gsub('.', '\.').length.

hth,  
Todd

> **···**
>
> On Wed, Dec 17, 2008 at 3:12 PM, Joe Peck \<joe@notsleepy.com\> wrote:
> 
> > Joe Peck wrote:
> > 
> > > I stumbled onto this thread trying to figure out a similar problem...
> > 
> > Dangit, I didn't mean to post the above message 🙂
> > 
> > Okay, I need to read a site name (like "joe.com") and put a \ in front  
> > of the period (so it would be "joe\.com"). I'm having trouble with  
> > this, I can't seem to get only ONE \ in front of the period...
> > 
> > > > "joe.com".gsub(/\./, "\\\.")
> > 
> > =\> "joe\\.com"
> > 
> > > > "joe.com".gsub(/\./, "\\\\.")
> > 
> > =\> "joe\\.com"
> > 
> > Anyone have a clue what to do? I'm probably missing something simple.

---

<div class="post-metadata">

**Author:** ![Joe\_Peck](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@Joe\_Peck](https://rubytalk.org/u/Joe_Peck)\
**Post date:** [18 December 2008 15:12 UTC](https://rubytalk.org/t/gsub-seems-unintuitive/44701/11 "2008-12-18T15:12:51Z")

</div>

Okay, those last two explanations helped clear up my confusion. Thanks.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
