RegEx stuff

J

Justin To

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!
 
T

Tachikoma

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!

try
a=a.to_s.gsub(c, "\\\\#{c}").to_a

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

Peña, Botp

RnJvbTogSnVzdGluIFRvIFttYWlsdG86dGVrbWNAaG90bWFpbC5jb21dIA0KIyA9PiBbImFcXF5i
XFxeXFxeYyZkZWYiXQ0KIyBIb3cgY29tZSB0aGUgJiBkb2VzIG5vdCBnZXQgc3ViYmVkIHdpdGgg
J1xcJicgPz8NCiMgSSdtIGJhc2ljYWxseSB0cnlpbmcgdG8gc3Vic3RpdHV0ZSBhbnkgc3BlY2lh
bCBjaGFyYWN0ZXIgd2l0aA0KIyAnXFwje3NwZWNpYWxfY2hhcmFjdGVyfScNCg0KaSdtIGEgYmxv
Y2sgZmFuLCBzbywNCg0KaXJiKG1haW4pOjA0ODowPiBiLmVhY2ggZG8gfGN8DQppcmIobWFpbik6
MDQ5OjEqICAgYT1hLnRvX3MuZ3N1YihjKXsiXFwje2N9In0udG9fYQ0KaXJiKG1haW4pOjA1MDox
PiBlbmQNCj0+IFsiXiIsICImIl0NCg0KaXJiKG1haW4pOjA1MjowPiBhDQo9PiBbImFcXF5iXFxe
XFxeY1xcJmRlZiJdDQoNCnVzaW5nIGJsb2NrIGZvcm0gb2YgZ3N1YiB3b250IGdpdmUgeW91IHBy
b2JsZW1zIG9uIGVzY2FwaW5nIHRoZSBlc2NhcGUuLg0KDQpraW5kIHJlZ2FyZHMgLWJvdHANCg==
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,662
Latest member
salsusa

Latest Threads

Top