Ok. After some tests, I get what you are saying.

The /g flag does not require that further matching must start past the end of the previous match--it just makes a request for any other unique matches. And the regex /(\w\w\w)*?TGA/ can act like the regex /TGA/ because a regex will happily match nothing for *.

But then why doesn't the /g flag cause this to match four times:

use strict; use warnings; use 5.010; my $str = 'aaaBBBcccTGA'; while ($str =~ /(?:\w\w\w)*?(TGA)/g) { say $1; say pos $str; }

Aren't there four unique matches:

1)  when (\w\w\w) is matched 0 times.
2)  when (\w\w\w) is matched 1 time.
3)  when (\w\w\w) is matched 2 times.
4)  when (\w\w\w) is matched 3 times.

That suggests that another match must end past the previous match--but that the next match doesn't have to start past the previous match.

My tests also show that starting the regex with a \A to anchor it to the beginning of the string will cause the regex to match only once:

use strict; use warnings; use 5.010; my $str = 'aaaBBBcccTGAddTGA'; while ($str =~ /\A(?:\w\w\w)*?(TGA)/g) { say $1; say pos $str; } --output:-- TGA 12

But then I would expect this to match twice, and it doesn't:

use strict; use warnings; use 5.010; my $str = 'aaaBBBcccTGAdddTGA'; while ($str =~ /\A(?:\w\w\w)*?(TGA)/g) { say $1; say pos $str; }

So I guess I don't have any idea what's going on.


In reply to Re^2: \G and regexes by 7stud
in thread \G and regexes by 7stud

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.