Re: Regex lookahead example not as stated in Camel 4th
thanks everyone for pointing out what I hadn't seen in the docs. On Wed, Jun 19, 2013 at 4:00 PM, Yitzchak Scott-Thoennes wrote: > On Wed, Jun 19, 2013 at 7:28 AM, Bob MacCallum > wrote: > > so, forgetting the typo for a moment, why doesn't it output > > 012 > > 012 > > 012 > > 012 > > ... > > ? > > > > From my reading of the 5.12 perlre docs, there is no mention of moving > the > > cursor along by one position after each match. > > Maybe that is a more general thing I wasn't aware of. > > It's not as simple as moving by one after each match; when it goes to > match it won't allow a 0-length match at the same position as the > previous 0-length match. > > > http://perldoc.perl.org/5.12.5/perlre.html#Repeated-Patterns-Matching-a-Zero-length-Substring >
Re: Regex lookahead example not as stated in Camel 4th
On Wed, Jun 19, 2013 at 7:28 AM, Bob MacCallum wrote: > so, forgetting the typo for a moment, why doesn't it output > 012 > 012 > 012 > 012 > ... > ? > > From my reading of the 5.12 perlre docs, there is no mention of moving the > cursor along by one position after each match. > Maybe that is a more general thing I wasn't aware of. It's not as simple as moving by one after each match; when it goes to match it won't allow a 0-length match at the same position as the previous 0-length match. http://perldoc.perl.org/5.12.5/perlre.html#Repeated-Patterns-Matching-a-Zero-length-Substring
Re: Regex lookahead example not as stated in Camel 4th
Bob MacCallum wrote: > From my reading of the 5.12 perlre docs, there is no mention of moving the > cursor along by one position after each match. > Maybe that is a more general thing I wasn't aware of. Yes, though only when the match was zero-length. This is done on the grounds that an infinite loop would be a relatively unhelpful outcome in such situations. The documentation for this feature in 5.12 is here: https://metacpan.org/module/LBROCARD/perl-5.12.4/pod/perlre.pod#Repeated-Patterns-Matching-a-Zero-length-Substring but I suggest reading the latest version, as it's been expanded a little (without any change to the underlying feature): https://metacpan.org/module/RJBS/perl-5.18.0/pod/perlre.pod#Repeated-Patterns-Matching-a-Zero-length-Substring -- Aaron Crane ** http://aaroncrane.co.uk/
Re: Regex lookahead example not as stated in Camel 4th
On 2013-06-19 Bob MacCallum wrote: > so, forgetting the typo for a moment, why doesn't it output > 012 > 012 > 012 > 012 > ... > ? > > From my reading of the 5.12 perlre docs, there is no mention of > moving the cursor along by one position after each match. Maybe > that is a more general thing I wasn't aware of. L Summary: since C would be an infinite loop, Perl breaks it for you, advancing the C by one after each (0-len) match. -- Dakkar - GPG public key fingerprint = A071 E618 DD2C 5901 9574 6FE2 40EA 9883 7519 3F88 key id = 0x75193F88 Beware of Programmers who carry screwdrivers. -- Leonard Brandwein signature.asc Description: PGP signature
Re: Regex lookahead example not as stated in Camel 4th
I was thinking you'd have to use something like this perl -wE 'say for "0123456789" =~ /(\d(?=(\d{2})))/g' 0 12 1 23 2 34 3 45 4 56 5 67 6 78 7 89 obviously with some post-processing required. On Wed, Jun 19, 2013 at 3:28 PM, Bob MacCallum wrote: > so, forgetting the typo for a moment, why doesn't it output > 012 > 012 > 012 > 012 > ... > ? > > From my reading of the 5.12 perlre docs, there is no mention of moving the > cursor along by one position after each match. > Maybe that is a more general thing I wasn't aware of. > > > > > > > On Wed, Jun 19, 2013 at 3:02 PM, gvim wrote: > >> On 19/06/13 14:52, Abigail wrote: >> >> >>> That's not a lookahead assertion. This is: >>> >>>$ perl -wE 'say for "0123456789" =~ /(?=(\d{3}))/g' >>>012 >>>123 >>>234 >>>345 >>>456 >>>567 >>>678 >>>789 >>>$ >>> >>> >>> >> So there's a typo on p.248 >> >> gvim >> > >
Re: Regex lookahead example not as stated in Camel 4th
so, forgetting the typo for a moment, why doesn't it output 012 012 012 012 ... ? >From my reading of the 5.12 perlre docs, there is no mention of moving the cursor along by one position after each match. Maybe that is a more general thing I wasn't aware of. On Wed, Jun 19, 2013 at 3:02 PM, gvim wrote: > On 19/06/13 14:52, Abigail wrote: > > >> That's not a lookahead assertion. This is: >> >>$ perl -wE 'say for "0123456789" =~ /(?=(\d{3}))/g' >>012 >>123 >>234 >>345 >>456 >>567 >>678 >>789 >>$ >> >> >> > So there's a typo on p.248 > > gvim >
Re: Regex lookahead example not as stated in Camel 4th
On Wed, Jun 19, 2013 at 03:02:32PM +0100, gvim wrote: > On 19/06/13 14:52, Abigail wrote: > > > That's not a lookahead assertion. > > So there's a typo on p.248 Yes, and it's listed in the errata on the publisher's Web site: http://oreilly.com/catalog/errata.csp?isbn=9780596004927 Tom
Re: Regex lookahead example not as stated in Camel 4th
Quoting gvim : On 19/06/13 14:52, Abigail wrote: That's not a lookahead assertion. This is: $ perl -wE 'say for "0123456789" =~ /(?=(\d{3}))/g' 012 123 234 345 456 567 678 789 $ So there's a typo on p.248 Patches welcome - http://oreilly.com/catalog/errata.csp?isbn=9780596004927 Dave...
Re: Regex lookahead example not as stated in Camel 4th
On 19/06/13 14:52, Abigail wrote: That's not a lookahead assertion. This is: $ perl -wE 'say for "0123456789" =~ /(?=(\d{3}))/g' 012 123 234 345 456 567 678 789 $ So there's a typo on p.248 gvim
Re: Regex lookahead example not as stated in Camel 4th
I'd say typo in the book. A "look-ahead assertion" looks like C<< (?= ) >> (note C<=> instead of C<:>). C<< (?: ) >> is just a non-capturing group. $ perl -E 'say for "0123456789" =~ /(?=(\d{3}))/g' 012 123 234 345 456 567 678 789 -- Dakkar - GPG public key fingerprint = A071 E618 DD2C 5901 9574 6FE2 40EA 9883 7519 3F88 key id = 0x75193F88 Storms beget storms. Rage begets rage. Revenge begets revenge. Wars beget wars. -- Bene Gesserit Conundrum signature.asc Description: PGP signature
Re: Regex lookahead example not as stated in Camel 4th
On Wed, Jun 19, 2013 at 02:36:14PM +0100, gvim wrote: > The 4th edition of the Camel states: > > * > "0123456789" =~ /(\d{3})/g > > returns only three strings: 012, 345, and 678. By wrapping the capture > group with a lookahead assertion: > > "0123456789" =~ /(?:(\d{3}))/g > > you now retrieve all of 012, 123, 234, 345, 456, 567, 678, and 789. > > ** > > Howevery, my test with perl 5.14.2 on Ubuntu 13.04 does not concur: > > $ perl -E 'say for "0123456789" =~ /(\d{3})/g' > 012 > 345 > 678 > > $ perl -E 'say for "0123456789" =~ /(?:(\d{3}))/g' > 012 > 345 > 678 That's not a lookahead assertion. This is: $ perl -wE 'say for "0123456789" =~ /(?=(\d{3}))/g' 012 123 234 345 456 567 678 789 $ Regards, Abigail
Regex lookahead example not as stated in Camel 4th
The 4th edition of the Camel states: * "0123456789" =~ /(\d{3})/g returns only three strings: 012, 345, and 678. By wrapping the capture group with a lookahead assertion: "0123456789" =~ /(?:(\d{3}))/g you now retrieve all of 012, 123, 234, 345, 456, 567, 678, and 789. ** Howevery, my test with perl 5.14.2 on Ubuntu 13.04 does not concur: $ perl -E 'say for "0123456789" =~ /(\d{3})/g' 012 345 678 $ perl -E 'say for "0123456789" =~ /(?:(\d{3}))/g' 012 345 678 gvim