LPM: regexp question

Rich Bowen rbowen at rcbowen.com
Tue Dec 21 07:33:27 CST 1999


Matt Cashner wrote:
> 
> all righty then. what i'm trying to do is to determine if the words
> "is" and "are" are  one of the first five words in a sentence. the code
> i'm using now looks like this: (yes, its ugly and newbie-ish)

This looks fun!
 
> my ($stu_foo,$stu_blah, at stu_search, at stu_refs,$stu_term);
> $stu_term = $orig_Y;
> @stu_search = ('is','are');
> $theMsg =~ /(\w*)\s*(\w*)\s*(\w*)\s*(\w*)\s*(\w*).*?/;
> @stu_refs = ($1,$2,$3,$4,$5);
> foreach $stu_blah (@stu_refs) {
>    foreach $stu_foo (@stu_search) {
>       if ($stu_blah =~ /\b$stu_foo\b/ or $stu_blah =~ /\b$stu_term\b/) {
>           $theMsg = "<reply> $who: $theMsg";
>           goto OUT;
>       }
>    }
> }
> OUT:
> 
> anyway, is there a better way to do this? this seems awfully hackish to
> me... thanks for your help all...

My suggestion would be some variation on ...

@stu_search = qw(is are);
%words = map {$_=>1} (split (/\s+/, $theMsg, 5);
for (@stu_search) {
	$match=1 if $words{$_}
}

Rich
-- 
http://www.ApacheUnleashed.com/
Lexington Perl Mongers - http://lexington.pm.org/
PGP Key - http://www.rcbowen.com/pgp.txt



More information about the Lexington-pm mailing list