Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
gsub and slash-ampersand in the substitution string
Jani Patokallio
10/9/2006 11:58:00 AM
Greetings,
I'm trying to do something that should be very simple: stick a "\" in
front of
every instance of "&" in a string. However, the obvious code...
"this&that".gsub!("&", "*")
--> "this*that" # OK!
"this&that".gsub!("&", "\\&")
--> "this&that" # Wrong
...doesn't work, because "\&" means "last match" in gsub substitution
strings.
How can I escape this? I experimentally determined that entering
"\\\\\\&"
(that's six backslashes) gets the desired result, but I don't really
understand why. Is there a less obscure way of doing this?
Cheers,
-jani
--
Posted via
http://www.ruby-...
.
2 Answers
Esteban Manchado Velázquez
10/9/2006 12:05:00 PM
0
On Mon, Oct 09, 2006 at 08:58:09PM +0900, Jani Patokallio wrote:
> Greetings,
>
> I'm trying to do something that should be very simple: stick a "\" in
> front of
> every instance of "&" in a string. However, the obvious code...
>
> "this&that".gsub!("&", "*")
> --> "this*that" # OK!
>
> "this&that".gsub!("&", "\\&")
> --> "this&that" # Wrong
>
> ...doesn't work, because "\&" means "last match" in gsub substitution
> strings.
> How can I escape this? I experimentally determined that entering
> "\\\\\\&"
> (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 '&'...
--
Esteban Manchado Velázquez <zoso@foton.es> -
http://ww...
EuropeSwPatentFree -
http://EuropeSwPatentFree.his...
Jano Svitok
10/9/2006 2:45:00 PM
0
this was discussed several times, try searching for gsub and \\ or
escaping or something similar. \& is the last match, and \\ is the
whole match. so you need to escape both. The correct solutions to this
'quiz' are posted somewhere, just google it ;-)
On 10/9/06, Esteban Manchado Velázquez <zoso@foton.es> wrote:
> On Mon, Oct 09, 2006 at 08:58:09PM +0900, Jani Patokallio wrote:
> > Greetings,
> >
> > I'm trying to do something that should be very simple: stick a "\" in
> > front of
> > every instance of "&" in a string. However, the obvious code...
> >
> > "this&that".gsub!("&", "*")
> > --> "this*that" # OK!
> >
> > "this&that".gsub!("&", "\\&")
> > --> "this&that" # Wrong
> >
> > ...doesn't work, because "\&" means "last match" in gsub substitution
> > strings.
> > How can I escape this? I experimentally determined that entering
> > "\\\\\\&"
> > (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 '&'...
>
> --
> Esteban Manchado Velázquez <zoso@foton.es> -
http://ww...
> EuropeSwPatentFree -
http://EuropeSwPatentFree.his...
>
>
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
gsub and slash-ampersand in the substitution string
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password