Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Answer: How do I find the Nth occurrence of a pattern?

by poolpi (Hermit)
on Oct 26, 2012 at 14:32 UTC ( #1001091=categorized answer: print w/replies, xml ) Need Help??

Q&A > regular expressions > How do I find the Nth occurrence of a pattern? - Answer contributed by poolpi

With split

my $string = "12 34 56 78 90 98 76 54 32 10"; my $n = 3; print+ (split( /[^\d+]/, $string))[$n-1]; # output => 56 my $string ='abcabbcabbbbcabcabbcab'; my $n = 3; print+ (split( /[^(?:ab+)]/, $string ))[$n-1]; # output => abbbb

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
NodeReaper updates his toe tag collection database

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2017-10-20 16:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (264 votes). Check out past polls.

    Notices?