Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: counting overlapping patterns

by merlyn (Sage)
on Feb 18, 2005 at 23:33 UTC ( #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
Node Status?
node history
Node Type: note [id://432570]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2019-11-18 19:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (92 votes). Check out past polls.

    Notices?