⭐ in reply to How do I find the Nth occurrence of a pattern?
The regex is: find the pattern (optionally preceded by something that isn't the pattern) n times. The pattern is in parens, so the final match will be returned.$_='abcabbcabbbbcabcabbcab'; my $n = 3; ## Find the $nth occurrence my $pat = qr/ab+/; ## of this pattern my ($NthMatch) = /(?:.*?($pat)){$n}/; print "Match #$n looks like $NthMatch\n";
|
|---|