in reply to Lookahead assertion
Look-ahead is (?= ), not (?: ) !
13:46 >perl -wMstrict -MData::Dump -E "my $foo = '123456789'; my @w1 = + ($foo =~ /(?=(\d{3}))/g); dd \@w1;" [123, 234, 345, 456, 567, 678, 789] 13:46 >
Update: The use of (?: ) instead of (?= ) appears to be a misprint in the Camel Book.
The following explanation from that same section (page 248) is worth noting (at least, it helped me to understand what is going on):
When the engine sees that it should try again because of the /g, it steps one character past where last it tried.
This explains why the final . in BrowserUk’s solution is not strictly necessary. But BrowserUk’s regex can be usefully generalised to step forward an arbitrary number of characters. For example, to capture 3 digits and then step forward 2 characters:
17:56 >perl -wMstrict -MData::Dump -E "my $foo = '123456789012345'; my + @w1 = ($foo =~ /(?=(\d{3})).{2}/g); dd \@w1;" [123, 345, 567, 789, 901, 123, 345] 18:09 >
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Lookahead assertion
by BrowserUk (Patriarch) on Oct 17, 2013 at 08:29 UTC | |
by talexb (Chancellor) on Oct 17, 2013 at 11:39 UTC | |
|
Re^2: Lookahead assertion
by talexb (Chancellor) on Oct 17, 2013 at 11:35 UTC |