Well of course I agree with Discipulus, because I do love debuggex :)

Here is something that you can try to understand what happens:

use v5.14; say "With the ?"; 'AATCGTTGAATGCAATGACATGAC' =~ / (\w\w\w)*? (?=(?{say "Checking if <$&> is followe +d by TGA"})) # Print everything that matched before that point TGA/x; say "Match: $&"; say "\nWithout the ?"; 'AATCGTTGAATGCAATGACATGAC' =~ / (\w\w\w)* (?=(?{say "Checking if <$&> is followe +d by TGA"})) # Print everything that matched before that point TGA/x; say "Match: $&";
You don't have to understand how the second line of the regex works, it just prints debug information on the current state of the regex :). Do note that I have changed your sample input so that there are two different "TGA" at a multiple of three position.

In both case, (\w\w\w)+ is a loop that reads three characters at a time. The difference is that in the first case, each times it reads three characters it lets the last part of the regex test the string (check if it is followed by TGA), if the test failed, three new characters are read and the test is ran again. The (\w\w\w)* loop of the second regex though, keeps reading characters as long are there are three characters to read, and it only lets the last part of the regex be checked after it is done, if the test fails, it goes back (backtracks) one iteration, and tries again.

The /g simply memories the position of the last successful match, and starts reading from there on the next attempt.


In reply to Re: Understanding a portion on the Perlretut by Eily
in thread Understanding a portion on the Perlretut by BlueStarry

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.