Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

by vroom (His Eminence)
on Jan 18, 2000 at 23:44 UTC ( [id://2151]=note: print w/replies, xml ) Need Help??


in reply to How do I find the Nth occurrence of a pattern?

Use the /g (global) to find all occurrences in a string. Place the matching statement within a while loop and count until you get to the wanted number.
$n=5; #want the 5th occurrence of a group of 5 numbers $count=0; while(/(\d{5})/g){ if(++$count==$n){ print "The $n\th occurrence was $1\n"; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://2151]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-16 06:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found