$word = "alphabet";
$string = "and她r在ut安ith地";
if($word!~ /$string/){
#do whatever
}
As you see the problem is that the 'a' will match any word with an 'a' where as I just want it to match 'a' if word is equal to 'a' exactly. I'd like to do this without looping through to test the word against each individually with eq if possible.
Thanks in advance,
Jerry
$word = "alphabet";
$string = "and她r在ut安ith地lphfabet地";
if ( $string =~ /\b\Q$word\E\b/ ) {
## we got a match
}
\bSearched for word boundaries.
\bis the place between a
\wcharacter and a
\Wcharacter.
\Qand
\Eallow to use variables between them. Otherwise Perl will literaly look for a dollar sign followed by 'word'.
Suggest to read: Perl Regex - a tiny introduction [anaesthetist.com]