# How to use regexp to replace character with backslash plus t

**URL:** <https://www.ruby-forum.com/t/how-to-use-regexp-to-replace-character-with-backslash-plus-t/76430>\
**Category:** Ruby\
**Created:** [November 18, 2006, 6:37pm UTC](https://www.ruby-forum.com/t/how-to-use-regexp-to-replace-character-with-backslash-plus-t/76430 "2006-11-18T18:37:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![madiba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@madiba](https://www.ruby-forum.com/u/madiba)\
**Post date:** [November 18, 2006, 6:37pm UTC](https://www.ruby-forum.com/t/how-to-use-regexp-to-replace-character-with-backslash-plus-t/76430/1 "2006-11-18T18:37:04Z")

</div>

Hello,

1.8.5.

I want to ‘correct’ filenames for unix shell so I want to preceed  
any let’s say & with backslash. Here is code translated from my  
original script in Perl – end of the function

return filename.gsub(/([;!&%$’])/,’\\1’)

However it doesn’t do anything good. If I put space in the middle  
\ \1

the replacament is correctly recognized. But I don’t want to use  
space in the middle. What’s wrong here?

have a nice day  
bye

---

<div class="post-metadata">

**Author:** ![madiba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@madiba](https://www.ruby-forum.com/u/madiba)\
**Post date:** [November 18, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/how-to-use-regexp-to-replace-character-with-backslash-plus-t/76430/2 "2006-11-18T19:07:08Z")

</div>

Maciej P. wrote:

> However it doesn’t do anything good. If I put space in the middle  
> \ \1
> 
> the replacament is correctly recognized. But I don’t want to use  
> space in the middle. What’s wrong here?

* * *

#!/usr/bin/ruby -w

data = “;!&%$”

data.gsub!(%r{([;!&%$’])},’\\\1’)

## puts data

Output:

;!&%$

---

<div class="post-metadata">

**Author:** ![madiba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@madiba](https://www.ruby-forum.com/u/madiba)\
**Post date:** [November 18, 2006, 7:37pm UTC](https://www.ruby-forum.com/t/how-to-use-regexp-to-replace-character-with-backslash-plus-t/76430/3 "2006-11-18T19:37:12Z")

</div>

On Sat, 18 Nov 2006 10:02:59 -0800, Paul L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> data.gsub!(%r{([;!&%$’])},‘\\\1’)

Thank you. But what is the interpretation of 5! backshlashes. First  
two to achieve one textual backslash – the last one, to get the  
on-fly variable. And the rest two?

have a nice day  
bye

---

<div class="post-metadata">

**Author:** ![madiba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@madiba](https://www.ruby-forum.com/u/madiba)\
**Post date:** [November 18, 2006, 8:15pm UTC](https://www.ruby-forum.com/t/how-to-use-regexp-to-replace-character-with-backslash-plus-t/76430/4 "2006-11-18T20:15:17Z")

</div>

On 18 Nov 2006 10:47:44 -0800, “Phrogz” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Please search the mailing list archive - this question comes up at  
> least once a month.

Thank you, but as you can see – google – not in that context – so  
often. I found ‘answer’ at RubyCentral, but I am still not convinced  
it works properly. Why  
\ \1  
is ok, but  
\\1  
not? If there is some kind of two-pass parsing – as RubyCentral  
explains  
\ \1  
should be converted to  
\1  
or even  
1  
but it is not.

It looks like some too greedy substitution/parsing algorithm – I mean  
at first the two first backshlashes are grouped together, then the  
second and the third.

have a nice day  
bye

---

<div class="post-metadata">

**Author:** ![madiba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@madiba](https://www.ruby-forum.com/u/madiba)\
**Post date:** [November 18, 2006, 7:51pm UTC](https://www.ruby-forum.com/t/how-to-use-regexp-to-replace-character-with-backslash-plus-t/76430/5 "2006-11-18T19:51:14Z")

</div>

Maciej P. wrote:

> Thank you. But what is the interpretation of 5! backshlashes. First  
> two to achieve one textual backslash – the last one, to get the  
> on-fly variable. And the rest two?

Please search the mailing list archive - this question comes up at  
least once a month.

Try this:  
[http://groups.google.com/group/comp.lang.ruby/search?group=comp.lang.ruby&q=backslash&qt\_g=1](http://groups.google.com/group/comp.lang.ruby/search?group=comp.lang.ruby&q=backslash&qt_g=1)

---

<div class="post-metadata">

**Author:** ![madiba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@madiba](https://www.ruby-forum.com/u/madiba)\
**Post date:** [November 19, 2006, 3:25am UTC](https://www.ruby-forum.com/t/how-to-use-regexp-to-replace-character-with-backslash-plus-t/76430/6 "2006-11-19T03:25:59Z")

</div>

Paul L. wrote:

> > return filename.gsub(/([;!&%$’])/,’\\1’)  
> > data = “;!&%$”
> 
> data.gsub!(%r{([;!&%$’])},’\\\1’)
> 
> ## puts data
> 
> Output:
> 
> ;!&%$

irb(main):055:0\> puts “;!&%$”.gsub( /[;!&%$]/, ‘\\&’ )  
;!&%$

irb(main):008:0\> puts “;!&%$”.gsub( /[;!&%$]/ ){ “\” + $& }  
;!&%$

irb(main):009:0\> puts “;!&%$”.gsub( /[;!&%$]/ ){ “\#{ $& }” }  
;!&%$

irb(main):011:0\> puts “;!&%$”.gsub( /([;!&%$])/ ){ “\#{ $1 }” }  
;!&%$

---

<div class="post-metadata">

**Author:** ![madiba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@madiba](https://www.ruby-forum.com/u/madiba)\
**Post date:** [November 19, 2006, 4:22am UTC](https://www.ruby-forum.com/t/how-to-use-regexp-to-replace-character-with-backslash-plus-t/76430/7 "2006-11-19T04:22:02Z")

</div>

William J. wrote:

/ …

> ;!&%$  
> Yes, except that I hate using those Perlish globals. Just a personal  
> thing.
