SPUG: Metacharacters in variables in regular expressions
John W. Krahn
jwkrahn at shaw.ca
Mon Jan 12 16:22:31 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?
>
[ snip ]
>
> So this is what I want, right?:
>
> if($some_earlier_var =~ /\Q$answer\E/) { ### destroy the universe ### }
Yes, or you could do it like this:
if(index{$some_earlier_var, $answer) >= 0) { ### destroy the universe ### }
John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
More information about the spug-list
mailing list