Perl Monk, Perl Meditation | |
PerlMonks |
Re: how to count the number of repeats in a string (really!) [regexp solution]by lodin (Hermit) |
on Nov 14, 2007 at 18:59 UTC ( [id://650826]=note: print w/replies, xml ) | Need Help?? |
This is a perfect task for the regex engine. A more generalized version where you can specify the minimum substring length and minimum number of occurances is
lodin Update: While writing this, ikegami posted a very similar-looking reply. While they look very much alike they work quite differently. ikegami's work by requiring that each match is repeated further into the string, and then goes on to count all those successive matches kind of like a global match. Mine does the counting right away, and then forces the engine to not count those again. So they work rather opposite of each other. I did a shallow benchmark. It seems that mine is a slight favourite (5-10%) in many, but not all, situations. I also get the impression that ikegami's scales slightly better, see Re^5: how to count the number of repeats in a string (really!). Update 2: Added the comment in the regex. Update 3: Added the generalized version.
In Section
Seekers of Perl Wisdom
|
|