SPUG: Metacharacters in variables in regular expressions

Jacinta Richardson jarich at perltraining.com.au
Mon Jan 12 15:42:54 PST 2009


Christopher Howard wrote:
> I wanted to do something like this:
> 
> my $answer = <>;
> chomp $answer;
> if($some_earlier_var =~ /$answer/) { ### destroy the universe ### }
> 
> But I need $answer to be 'safe', so the metacharacters aren't
> interpreted as such in the regex. What is the best thing to do here, then?

> So this is what I want, right?:
> 
> if($some_earlier_var =~ /\Q$answer\E/) { ### destroy the universe ### }

Yes, that's exactly what you want.

	J

-- 
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +61 3 9354 6001        |
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
 (il),-''  (li),'  ((!.-'             |   www.perltraining.com.au   |


More information about the spug-list mailing list