perlquestion
talexb
<p>I've been tinkering around with regular expressions, and in particular lookahead assertions. The examples on page 248 of the Camel (fourth edition) say that this code
<c>
"0123456789" =~ /(\d{3})/g;
</c>
returns only three strings, '012', '345' and '678', since the regex engine moves past the pattern it's found each time. I tried this in the debugger, and confirmed the behaviour:
<c>
main::(-e:1): 1
DB<1> $foo = "123456790"
DB<2> @w1 = ( $foo =~ /(\d{3})/g );
DB<3> x @w1
0 123
1 456
2 790
</c>
</p>
<p>
Now, I'm interested in a regex that also finds '123' and '234' -- I want to tell the regex engine to reset to where it found the last match, plus one character. The Camel appears to say that the next example does this, with the lookahead assertion as follows:
<c>
"0123456789" =~ /(?:(\d{3}))/g;
</c>
Brimming with energy and thrilled at my discovery, I try that out in the debugger:
<c>
DB<4> @w2 = ( $foo =~ /(?:(\d{3}))/g );
DB<5> x @w2
0 123
1 456
2 790
</c>
Nope. Can someone suggest what I've done wrong?
</p>
<p><b>Update:</b> As noted below, this is a typo, as recorded on the [http://oreilly.com/catalog/errata.csp?isbn=9780596004927|errata page] for this book. I'll be going through the rest of the errata and making updates to my Programming Perl (4th edition) later today.</p>
<div class="pmsig"><div class="pmsig-131279">
<p>Alex / [talexb] / Toronto</p>
<p><small>Thanks PJ. We owe you so much. <a href="http://www.groklaw.net">Groklaw</a> -- RIP -- 2003 to 2013.</small></p>
</div></div>