Sunday, December 04, 2005

Perl: Find the Nth Occurrence of a match

while(/(\w+)\s+fish\b/gi){
if(++$count == $WANT){
# do what ever you want to do
}
}

\w+ match a word
\s+ 1 or n space
\b word boundary
/g gloal match/substitue as often as possible.
/i ignore case

0 Comments:

Post a Comment

<< Home