CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
s/// : Why doesn't this work?

 



Zippy1970
Novice

Mar 4, 2013, 5:00 AM

Post #1 of 3 (11466 views)
s/// : Why doesn't this work? Can't Post


Code
my $str = '$1$'; 
my $key = '$1$ Hello World!';
$key =~ s/$str//;
print "$key\n";

Output:

$1$ Hello World!


As you can see, it's not removing '$1$'. This behavior is new to me. What am I missing here?


(This post was edited by Zippy1970 on Mar 4, 2013, 5:00 AM)


rovf
Veteran

Mar 4, 2013, 5:39 AM

Post #2 of 3 (11462 views)
Re: [Zippy1970] s/// : Why doesn't this work? [In reply to] Can't Post

Because $ has a special meaning in a regexp (and as a sigil).

Use


Code
my $str = quotemeta('$1$');


instead.


Zippy1970
Novice

Mar 4, 2013, 6:56 AM

Post #3 of 3 (11452 views)
Re: [rovf] s/// : Why doesn't this work? [In reply to] Can't Post

Right after I posted my original question, I found out this works as well:


Code
my $str = '$1$';  
my $key = '$1$ Hello World!';
$key =~ s/\Q$str\E//;
print "$key\n";


Which of course does the exact same thing. :)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives