Link to home
Start Free TrialLog in
Avatar of webcs
webcs

asked on

Detecting a word from a string in Perl

Detecting a word from a string in Perl

give variable $mysentence = "This house is red and not white";

Need a quick routine to check if a word is present in the variable and flag it if it is...

IE....if the word red is in the sentence then wordflag=1 else wordflag=0

Would also be nice if it could handle any case.

thanks
ASKER CERTIFIED SOLUTION
Avatar of dorward
dorward

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zyloch
To be even more careful, if you want to test the word case insensitive, do this: [code from dorward above]

if ($mysentence =~ /word/i) {$wordflag=1;} else {$wordflag=0;}

--Zyloch
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial