Rob Dixon wrote:

> Hi Scott, Francesco.
>
> Scott R. Godin wrote:
> > Francesco Del Vecchio wrote:
> >
> > > suppose this:
> > > ======================================
> > > $string 'I saw Roger and I said :roger? what the @*$!';
> > >
> > > $var1 = "roger? what the @*$!";
> > > $var2 = "Hi roger...nice to meet you";
> > >
> > > $string=~ s/$var1/$var2/;
> > > =======================================
> > >
> > > I'm having problems....due (i suppose) to the special chars in the
> > > $var1 string the s/// don't match anything.
> > > What can I do to?
> >
> > s{\Q$var1\E}{$var2} is usually what you want, except that may very
> > well 'quote' out the $ in $var.
>
> I guess you mean $var2? The replacement expression will
> only be interpolated once. Any variable names embedded in the
> contents of $var2 will be copied verbatim.
>
> > I suspect what you really want is
> >
> > $var1 = qr{roger? what the @*$!};
>
> This won't work, I'm afraid. The $! will be interpolated
> unless the delimiters are single-quotes:
>
>     print (my $var1 = qr{roger? what the @*$!});
>
> output
>
>     (?-xism:roger? what the @*)
>
> > ( perldoc -f qr ) (perldoc perlre) and (perldoc perlop) for more
> > details.
>
> Maybe what is wanted is:
>
>     $var1 = quotemeta q{roger? what the @*$!};
>
> which is the equivalent of the \Q...\E construct, but
> applicable to an existing string.
>
> HTH,
>
> Rob

Hi Rob,

Well, almost there.  FWIW, the poison characters are the regex control characters 
[?*$]  The at symbol and exclamation mark worked fine.  I also encountered the
(?-xism:roger\? what the [EMAIL PROTECTED]) using qr.  My first try with quotemeta q() 
also had some issues:

#!/usr/bin/perl -w

use strict;

my $string;
$string = 'I saw Roger and I said :roger? what the @*$!';

my $var1 = quotemeta q(roger? what the @*$!);
my $var2 = quotemeta q(Hi roger...nice to meet you);

$string =~ s/$var1/$var2/e;
print $var1 ."\n";
print $string . "\n";

E:\d_drive\perlStuff\guests>sub_roger.pl
roger\?\ what\ the\ [EMAIL PROTECTED]
I saw Roger and I said :Hi\ roger\.\.\.nice\ to\ meet\ you

It does succeed in the substitution, though, the first trial using all the characters 
that did so.  So one more change, a backtrack:
my $var2 = 'Hi roger...nice to meet you';
and ...  Voila!:
E:\d_drive\perlStuff\guests>sub_roger.pl
roger\?\ what\ the\ [EMAIL PROTECTED]
I saw Roger and I said :Hi roger...nice to meet you

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to