PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Problem with String.gsub and \' as a replacement string
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Problem with String.gsub and \' as a replacement string

Réponse
 
LinkBack Outils de la discussion
Vieux 17/11/2007, 12h19   #1
ridcully
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Problem with String.gsub and \' as a replacement string

Hi,

I want to replace all occurences of a certain character in a string
with a backslash followed by a single quote. Sounds like a trivial
task, but this is what I get:

"this is a test".gsub( "a", "\\'" ) -> "this is test test"

What I want is "this is \' test".

Neither does this work:
"this is a test".gsub( "a", '\' + "'" )

No matter what I am doing, as soon as a backslash is followed by a
single quote in the replacement string, I am getting weird results.

Thanks for your !

Andreas
  Réponse avec citation
Vieux 17/11/2007, 12h22   #2
Alex Young
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem with String.gsub and \' as a replacement string

ridcully wrote:
> Hi,
>
> I want to replace all occurences of a certain character in a string
> with a backslash followed by a single quote. Sounds like a trivial
> task, but this is what I get:
>
> "this is a test".gsub( "a", "\\'" ) -> "this is test test"
>
> What I want is "this is \' test".
>
> Neither does this work:
> "this is a test".gsub( "a", '\' + "'" )
>
> No matter what I am doing, as soon as a backslash is followed by a
> single quote in the replacement string, I am getting weird results.

Backslashes are confusing. Try this:

puts "this is a test".gsub( "a", "\\\\'" )

--
Alex

  Réponse avec citation
Vieux 17/11/2007, 12h29   #3
ridcully
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem with String.gsub and \' as a replacement string

On 17 Nov., 12:22, Alex Young <a...@blackkettle.org> wrote:
> Backslashes are confusing. Try this:
>
> puts "this is a test".gsub( "a", "\\\\'" )
>
> --
> Alex


Thank you, you saved my day!

This really is confusing, because "\\'" gives me the correct result if
I don't use it with gsub:

puts "\\'" -> \'

Am I missing something here?

Andreas
  Réponse avec citation
Vieux 17/11/2007, 12h47   #4
Sebastian Hungerecker
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem with String.gsub and \' as a replacement string

ridcully wrote:
> On 17 Nov., 12:22, Alex Young <a...@blackkettle.org> wrote:
> > puts "this is a test".gsub( "a", "\\\\'" )

>
> Thank you, you saved my day!
>
> This really is confusing, because "\\'" gives me the correct result if
> I don't use it with gsub:
>
> puts "\\'" -> \'
>
> Am I missing something here?


"\\'" translates to a literal backslash followed by '. This is what gsub gets.
gsub then sees \' and replaces it with $' the same way it would replace \1
with $1. To tell it not to do that it has to get \\', so it knows it's not
supposed to treat \' as special. In order to archieve that you have to
write "\\\\'". Hope that cleared things up for you.


--
NP: Falkenbach - Vanadis
Jabber: sepp2k@jabber.org
ICQ: 205544826

  Réponse avec citation
Vieux 17/11/2007, 14h05   #5
botp
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem with String.gsub and \' as a replacement string

On Nov 17, 2007 7:20 PM, ridcully <google@ridcully.net> wrote:
> "this is a test".gsub( "a", "\\'" ) -> "this is test test"


many ways, eg

~> "this is a test".gsub("i",Regexp.escape("\\'"))
=> "th\\'s \\'s a test"

~> "this is a test".gsub("i"){"\\'"}
=> "th\\'s \\'s a test"

the block form seems clean though.

kind regards -botp

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 20h37.


Édité par : vBulletin® version 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13884 seconds with 13 queries