Hi i feel we can do it in this way i have used my own example which is in the cookbook to explian it.
To generalise it better to find the N th match in a string, not just the first one. For example, you'd like to find the word preceding the third occurrence of "fish":
One fish two fish red fish blue fish
Solution
Use the /g modifier in a while loop, keeping count of matches:
The third fish is a red one.$WANT = 3; $count = 0; while (/(\w+)\s+fish\b/gi) { if (++$count == $WANT) { print "The third fish is a $1 one.\n"; # Warning: don't `last' out of this loop } }
Or use a repetition count and repeated pattern like this:
/(?:\w+\s+fish\s+){2}(\w+)\s+fish/i;
Code tags added by GrandFather
In reply to Re: Finding duplicate words in a paragraph
by msk_0984
in thread Finding duplicate words in a paragraph
by perladdict
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |