Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Problems? Is your data what you think it is?
 
PerlMonks  

Re: counting overlapping patterns

by saintmike (Vicar)
on Feb 18, 2005 at 15:19 UTC ( [id://432495]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to counting overlapping patterns

You need to tell the regex engine to pick up after the last match, using the \G anchor:
my $count = 0; my $string = "AAAA"; $count++ while $string =~ /\GAA/gc;
Argh, sorry, I was wrong. Check out this thread instead.

Replies are listed 'Best First'.
Re^2: counting overlapping patterns
by Anonymous Monk on Feb 18, 2005 at 15:25 UTC
    Ah... the \G anchor, of course.... it doesn't seem to work for me. Still getting 2. Are my cut-n-paste skills poor?
Re^2: counting overlapping patterns
by holli (Abbot) on Feb 18, 2005 at 15:25 UTC
    This returns 2. Reasonable because the position of the last match is right after the 2nd "A".
    I´d like to be disproven, but i think this cannot be solved by a simple regex.
    Disproven ,)


    holli, /regexed monk/
Re^2: counting overlapping patterns
by dave_the_m (Monsignor) on Feb 18, 2005 at 15:28 UTC
    $count++ while $string =~ /\GAA/gc;
    Er, no, the \G there serves no purpose as that's the default behaviour anyway. On this other hand this will work, by only consuming the first character of the match:
    $count++ while $string =~ /A(?=A)/g;

    Dave.

      Dave you truly are "the m". That works as promised. Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://432495]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.