RegEx stuff

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 via http://www.ruby-forum.com/.

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/.

# => ["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]

Great thanks!

···

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