LPM: regexp question
Rich Bowen
rbowen at rcbowen.com
Tue Dec 21 07:57:28 CST 1999
llang at baywestpaper.com wrote:
>
> The following works for what you're trying to do, but it assumes (maybe
> wrongly) that the words in your sentence are only separated by single
> spaces. I guess you could remove multiple spaces easily enough before
> running it through.
>
> #======================================
> $sent="This is a test of the first five words.";
> @s=split(/ /,$sent);
> pop(@s) while (@s>5);
Could potentially take a VERY long time. What if $sent is the complete
works of Shakespeare? Better to unshift for (1..5), I would think. Or
join @s[0..4]
> if (join(" ", at s) =~ /\bis|are\b/) {
> print "Do stuff here...";
> }
Hmm. So combining yours and mine ...
if ( join " " (split (/\s+/, $sent, 5) ) ) =~ /\bis|are\b) {
print "Do stuff here ... ";
}
:-)
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