Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Understanding a portion on the Perlretut

by Eily (Monsignor)
on Dec 09, 2015 at 13:28 UTC ( [id://1149757]=note: print w/replies, xml ) Need Help??


in reply to Understanding a portion on the Perlretut

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1149757]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-03-28 14:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found