And in the spirit of TIMTOWTDI:
my $string = "The quick brown fox jumps over the lazy dog."; my $count = 0; $count++ while $string =~ /(the)/gi;
This is useful if, for example, you want to operate on a particular occurence of a match. For example, if you wanted to grab the second occurence of particular item in every line of log file:
while (<IN_FILE>) { my $count = 0; MATCH: while (/(\Q$some_string\E)/gi) { $count++; if ( 2 == $count ) { # do something last MATCH; } } }
Cheers,
Ovid
New address of my CGI Course.
Silence is Evil (feel free to copy and distribute widely - note copyright text)
In reply to Re: counting the number of occurrances of a word using regex
by Ovid
in thread counting the number of occurrances of a word using regex
by abhishes
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |