http://qs321.pair.com?node_id=1210924


in reply to Re: Seeking clarification on possible bug in regex using \G and /gc
in thread Seeking clarification on possible bug in regex using \G and /gc

Breaks for me on perl 5.26.
perl -E 'local $_ = "foo"; say "Start" if /\G foo/gcx; say "Mid" if /\ +G .*/gcx; say "End" if /\G \z/gcx' Start Mid
Should instead print
Start Mid End
Here is a more extended item looking at pos as well.
perl -E 'local $_ = "foo"; say "Start" if /\G foo/gcx; say pos; say "M +id" if /\G .*/gcx; say pos; say "End" if /\G \z/gcx; say "At End" if +pos == length' Start 3 Mid 3 At End
my @a=qw(random brilliant braindead); print $a[rand(@a)];

Replies are listed 'Best First'.
Re^3: Seeking clarification on possible bug in regex using \G and /gc
by choroba (Cardinal) on Mar 14, 2018 at 23:52 UTC
    > Should instead print

    Why? Using perl -Mre=debug will show you your mistake:

    ... Matching REx "\G \z" against "" 3 <foo> <> | 1:GPOS(2) 3 <foo> <> | 2:EOS(3) 3 <foo> <> | 3:END(0) Match possible, but length=0 is smaller than requested=1, failing! Match failed

    The length=1 request comes from the previously mentioned Repeated Patterns Matching a Zero-length Substring:

    The higher-level loops preserve an additional state between iterations: whether the last match was zero-length. To break the loop, the following match after a zero-length match is prohibited to have a length of zero.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,