Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by choroba (Cardinal)
on Mar 14, 2018 at 23:05 UTC ( [id://1210921]=note: print w/replies, xml ) Need Help??


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

I'm not able to reproduce the behaviour you describe. Tried in 5.18.2 and 5.27.7, code:
#! /usr/bin/perl use warnings; use strict; use feature qw{ say }; { local $_ = 'foo'; say '1 Start' if /\G foo/gcx; say '1 Mid' if /\G .*/gcx; say '1 End' if /\G \z/gcx; } { local $_ = 'foo'; say '2 Start' if /\G foo/gcx; say '2 Mid' if /\G .+/gcx; say '2 End' if /\G \z/gcx; }

Output:

1 Start 1 Mid 2 Start 2 End
($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,

Replies are listed 'Best First'.
Re^2: Seeking clarification on possible bug in regex using \G and /gc
by davido (Cardinal) on Mar 15, 2018 at 01:45 UTC

    Thanks choroba for setting me straight. If there's one thing that I keep forgetting to learn it's not to post regex questions or answers in haste. ;)

    You are correct. This:

    local $_ = 'foo'; m/\Gfoo/gc && say 'Matched foo'; m/\G.+/gc && say 'Matched dot star'; m/\G\z/gc && say 'Matched end of string';

    ...produces this:

    Matched foo Matched end of string

    And this:

    local $_ = 'foo'; m/\Gfoo/gc && say 'Matched foo'; m/\G.*/gc && say 'Matched dot star'; m/\G\z/gc && say 'Matched end of string';

    ...produces this:

    Matched foo Matched dot star

    ...indicating that end of string was consumed by dot star, though it's still a little odd because this also matches:

    local $_ = 'foo'; m/\Gfoo/gc && say 'Matched foo'; m/\G.*\z/gc && say 'Matched dot star and end of string';

    ...produces this:

    Matched foo Matched dot star and end of string

    So while it may be eluded to in the documentation it's not entirely unsurprising. :)


    Dave

Re^2: Seeking clarification on possible bug in regex using \G and /gc
by Rhandom (Curate) on Mar 14, 2018 at 23:24 UTC
    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)];
      > 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,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1210921]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (1)
As of 2024-04-16 15:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found