Pathologically Eclectic Rubbish Lister | |
PerlMonks |
Re: how to count the number of repeats in a string (really!)by oha (Friar) |
on Nov 14, 2007 at 16:34 UTC ( [id://650792]=note: print w/replies, xml ) | Need Help?? |
First of all, i will find the longest matching sequences possibile, which are in the following string xx, abc and ecd. (I use the zero-width lookahead to avoid to reset pos) Then I'll break those substrings in parts, if abc is repeated i suspect also bc is repeated, isn't it? Then I'll count the repetitions of only those substrings: The first loop find the repetitions, the second count them. if you want to get only 2 or more char substring, change $x+1 to $x+2. Oha Update: added regex quoting to the last re Update: shorter and print in order of findings:
Update: fix a bug in the above code, added a pos() relocation (see #fix)
In Section
Seekers of Perl Wisdom
|
|