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: Frequently Asked Questions:
How can I quote a variable to use in a regexp?

 



Jasmine
Administrator

Mar 15, 2001, 6:01 AM

Post #1 of 1 (20480 views)
How can I quote a variable to use in a regexp? Can't Post

How can I quote a variable to use in a regexp?

The Perl parser will expand $variable and @variable references in regular expressions unless the delimiter is a single quote. Remember, too, that the right-hand side of a s/// substitution is considered a double-quoted string (see the perlop manpage for more details). Remember also that any regexp special characters will be acted on unless you precede the substitution with \Q. Here's an example:



Code
    $string = "to die?"; 
$lhs = "die?";
$rhs = "sleep no more";

$string =~ s/\Q$lhs/$rhs/;
# $string is now "to sleep no more"

Without the \Q, the regexp would also spuriously match ``di''.



 
 


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

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