From: "Esteban Manchado Velázquez" Date: 2006-10-09T21:05:19+09:00 Subject: Re: gsub and slash-ampersand in the substitution string --/NkBOFFp2J2Af1nK Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Oct 09, 2006 at 08:58:09PM +0900, Jani Patokallio wrote: > Greetings, >=20 > I'm trying to do something that should be very simple: stick a "\" in=20 > front of > every instance of "&" in a string. However, the obvious code... >=20 > "this&that".gsub!("&", "*") > --> "this*that" # OK! >=20 > "this&that".gsub!("&", "\\&") > --> "this&that" # Wrong >=20 > ...doesn't work, because "\&" means "last match" in gsub substitution=20 > strings. > How can I escape this? I experimentally determined that entering=20 > "\\\\\\&" > (that's six backslashes) gets the desired result, but I don't really > understand why. Is there a less obscure way of doing this? I'm not sure, but I think that the problem is that you are using double quotes. So, the value you are passing is really the same as: '\\\&' Which, once you interpret the escape sequences, you have a literal '\' and a literal '&'... --=20 Esteban Manchado Vel=E1zquez - http://www.foton.es EuropeSwPatentFree - http://EuropeSwPatentFree.hispalinux.es --/NkBOFFp2J2Af1nK Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (GNU/Linux) iD8DBQFFKjrxhYgK5b1UDsERAlC8AJsFBEEKmaLIgyjCrc/sDYa+MD34ogCfchCt HEx/oeI+zaBbLiX7BWDmd1c= =Kypl -----END PGP SIGNATURE----- --/NkBOFFp2J2Af1nK--