how to safely substitute a "(", ")" or "[" 
Author Message
 how to safely substitute a "(", ")" or "["

Hi all,

this simple script is working nicely, except if I want to substitute a "(",
")" or "[".

#########################
#!/usr/local/bin/perl -w
use strict;

my $string = 'abd&6)31%34';
my $subs = 'abd';
my $new_string = $string;
$new_string  =~ s/$subs//;

print "$string has been substituted $subs:\n$new_string\n";
#########################
nrn1# perl test
abd&6)31%34 has been substituted abd:
&6)31%34

But now, if I replace $subs with ")", I get an error:

/")"/: unmatched () in regexp at test line 7.

How can I deal with this? I can escape it (make $subs = '\)'), but is there
another way? Any other characters I should be aware of?

I already tried:
nrn1> perldoc -q substitution
No documentation for perl FAQ keyword `substitution' found"

Thanks!

Christian



Sun, 31 Jul 2005 17:17:48 GMT  
 how to safely substitute a "(", ")" or "["

Quote:

> this simple script is working nicely, except if I want to substitute a "(",
> ")" or "[".
> $new_string  =~ s/$subs//;
> I can escape it (make $subs = '\)'), but is there
> another way?

   $new_string  =~ s/\Q$subs//;

see also:

   perldoc -f quotemeta

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Sun, 31 Jul 2005 17:40:33 GMT  
 how to safely substitute a "(", ")" or "["

:But now, if I replace $subs with ")", I get an error:

:/")"/: unmatched () in regexp at test line 7.

:How can I deal with this? I can escape it (make $subs = '\)'), but is there
:another way? Any other characters I should be aware of?

See the perlre man page for the \Q and \E modifiers. See also
the equivilent function, quotemeta .
--
Disobey all self-referential sentences!



Sun, 31 Jul 2005 17:43:08 GMT  
 how to safely substitute a "(", ")" or "["
X-Ftn-To: Christian Caron

Quote:

>print "$string has been substituted $subs:\n$new_string\n";
>#########################
>nrn1# perl test
>abd&6)31%34 has been substituted abd:
>&6)31%34

>But now, if I replace $subs with ")", I get an error:

>/")"/: unmatched () in regexp at test line 7.

>How can I deal with this? I can escape it (make $subs = '\)'), but is there
>another way? Any other characters I should be aware of?

\Q escapes chars for you, so you can:
$new_string  =~ s/\Q$subs\E//;
or just:
$new_string  =~ s/\Q$subs//;

--
Matija



Sun, 31 Jul 2005 17:44:24 GMT  
 how to safely substitute a "(", ")" or "["

Quote:

> > this simple script is working nicely, except if I want to substitute a
"(",
> > ")" or "[".

>    $new_string  =~ s/\Q$subs//;

> see also:

>    perldoc -f quotemeta

Thanks!

Christian



Sun, 31 Jul 2005 18:14:33 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. "character class ""bug""

2. system(("cp", "-Rf", "/tmp/a/*", "/tmp/b")); doesnt wrk

3. how to parse a "this", "that", "and the ", "other" file

4. s"\n+$"" s"\n*$""

5. "perlfreak", "i love perl", "perllover" or...???

6. how to parse "this", "that", "other"

7. split line like "a","b","c"

8. Looking for a Solaris "quot" replacement

9. Substitute across lines in "one liner"

10. substituting "@" with regexp

11. substituting with /g and simulating "lookbehind"

12. How to "$block_of_text<<"EOT" while "use strict"?

 

 
Powered by phpBB® Forum Software