# RegEx stuff

**URL:** <https://rubytalk.org/t/regex-stuff/47733>\
**Category:** ruby-talk\
**Created:** [16 July 2008 05:08 UTC](https://rubytalk.org/t/regex-stuff/47733 "2008-07-16T05:08:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Justin\_To](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@Justin\_To](https://rubytalk.org/u/Justin_To)\
**Post date:** [16 July 2008 05:08 UTC](https://rubytalk.org/t/regex-stuff/47733/1 "2008-07-16T05:08:12Z")

</div>

a = [["a^b^^"], ["c&def"]]  
b= []

a.to\_s.each\_byte { |byte| (b.push(byte.chr) if !b.include?(byte.chr)) if  
byte.chr.match(/\W+/) }

b.each do |c|  
&nbsp;&nbsp;a=a.to\_s.gsub(c, "\\#{c}").to\_a  
&nbsp;&nbsp;end

p a

=\> ["a\\^b\\^\\^c&def"]

How come the & does not get subbed with '\\&' ??

I'm basically trying to substitute any special character with  
'\\#{special\_character}'

THANKS!

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

---

<div class="post-metadata">

**Author:** ![Tachikoma](https://avatars.discourse-cdn.com/v4/letter/t/aeb1de/32.png) [@Tachikoma](https://rubytalk.org/u/Tachikoma)\
**Post date:** [16 July 2008 06:55 UTC](https://rubytalk.org/t/regex-stuff/47733/2 "2008-07-16T06:55:20Z")

</div>

try  
a=a.to\_s.gsub(c, "\\\\#{c}").to\_a

"\\" will present as '\'  
'\&' will present as '&'

> **···**
>
> On Jul 16, 1:08 pm, Justin To \<te...@hotmail.com\> wrote:
> 
> > a = [["a^b^^"], ["c&def"]]  
> > b=
> > 
> > a.to\_s.each\_byte { |byte| (b.push(byte.chr) if !b.include?(byte.chr)) if  
> > byte.chr.match(/\W+/) }
> > 
> > b.each do |c|  
> > a=a.to\_s.gsub(c, "\\#{c}").to\_a  
> > end
> > 
> > p a
> > 
> > =\> ["a\\^b\\^\\^c&def"]
> > 
> > How come the & does not get subbed with '\\&' ??
> > 
> > I'm basically trying to substitute any special character with  
> > '\\#{special\_character}'
> > 
> > THANKS!  
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [16 July 2008 08:15 UTC](https://rubytalk.org/t/regex-stuff/47733/3 "2008-07-16T08:15:21Z")

</div>

# =\> ["a\\^b\\^\\^c&def"]  
# How come the & does not get subbed with '\\&' ??  
# I'm basically trying to substitute any special character with  
# '\\#{special\_character}'

i'm a block fan, so,

irb(main):048:0\> b.each do |c|  
irb(main):049:1\* a=a.to\_s.gsub(c){"\\#{c}"}.to\_a  
irb(main):050:1\> end  
=\> ["^", "&"]

irb(main):052:0\> a  
=\> ["a\\^b\\^\\^c\\&def"]

using block form of gsub wont give you problems on escaping the escape..

kind regards -botp

> **···**
>
> From: Justin To [[mailto:tekmc@hotmail.com](mailto:tekmc@hotmail.com)]

---

<div class="post-metadata">

**Author:** ![Justin\_To](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@Justin\_To](https://rubytalk.org/u/Justin_To)\
**Post date:** [16 July 2008 18:37 UTC](https://rubytalk.org/t/regex-stuff/47733/4 "2008-07-16T18:37:58Z")

</div>

Great thanks!

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