|
Posted by Peter Ludikovsky on March 11, 2008, 4:14 am
Please log in for more thread options gazza67 wrote:
> Anybody,
>
> I want to use a regex in which the string for which we are searching
> is a variable. Is this possible - it doesnt seem to work for me.
>
> $a = "be";
>
> $b="to be or not to be";
> if ($b =~ m/$a/i )
> print "this should be true";
>
> $b = "apples and oranges";
> if ($b =~ m/$a/i )
> print "this should be false";
>
>
>
> Cheers
> Gary
1) Don't use $a & $b. Quote from 'perldoc perlvar':
Special package variables when using sort(), see "sort" in
perlfunc. Because of this specialness $a and $b don't need to
be declared (using use vars, or our()) even when using the
"strict 'vars'" pragma. Don't lexicalize them with "my $a" or
"my $b" if you want to be able to use them in the sort() com‐
parison block or function.
2) You can make a better distinction between matching text and matching
variables by enclosing your variables in curly brackets, eg:
$string =~ /$/;
HTH
/peter
|