Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: counting overlapping patterns

by merlyn (Sage)
on Feb 18, 2005 at 23:33 UTC ( [id://432570]=note: print w/replies, xml ) Need Help??


in reply to counting overlapping patterns

Here's a bizarre way of doing it, taking a hint from my Prolog studies (thanks Ovid!):
sub match_count { my ($string, $pattern) = @_; my $n = 0; $string =~ /$pattern(?{ $n++ })(?!)/; return $n; }
Essentially, match successfully, count it, but then fail the match (an empty string can always match, so negating that always fails).

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-23 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found