aeqr has asked for the wisdom of the Perl Monks concerning the following question:
trizen explained to me how this regex worked and this is what I understood:"ABCDEF" =~ /([A-Z]{3})(?{print "$1\n"})(?!)/;
First it matches ABC, then prints it, then fail, then backtracks to the position where the last match occured + 1.
Then when it runs again, it will match BCD this time. Thus this loop prints:
ABC BCD CDE DEF
However based on this reasoning, I struggle to understand the next example:
Here it matches AB first then prints it, then fails then backtracks. But here, instead of matching BC as I would expect it, it matches ABC. So after failing, it does not backtrack to pos+1 like in the previous example. And the loop prints:"ABCDEF" =~ /(\w{2,}?)(?{print "$1\n"})(?!)/;
Can someone explain me what is wrong in my assumption?AB ABC ABCD ABCDE ABCDEF BC BCD BCDE BCDEF CD CDE CDEF DE DEF EF
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: regex backtracking question
by AnomalousMonk (Archbishop) on Apr 24, 2014 at 00:49 UTC | |
by aeqr (Novice) on Apr 24, 2014 at 01:11 UTC | |
by AnomalousMonk (Archbishop) on Apr 24, 2014 at 01:34 UTC | |
by aeqr (Novice) on Apr 24, 2014 at 01:47 UTC | |
|
Re: regex backtracking question
by AnomalousMonk (Archbishop) on Apr 24, 2014 at 05:02 UTC | |
|
Re: regex backtracking question
by trizen (Hermit) on Apr 24, 2014 at 10:59 UTC | |
by aeqr (Novice) on Apr 24, 2014 at 14:41 UTC |