http://www.perlmonks.org?node_id=11155516


in reply to Precise backreference semantics in Perl regular expressions

My perl disagrees.

#!/usr/bin/perl use strict; #use warnings; for my $regexp ( qr/^((a*)b|a*b?d)*c\2$/, qr/^(a*b?d|(a*)b)*c\2$/, ) { for my $string (qw( aabadcaa aababdcaa )) { print "\n$string\n$regexp\n"; print $string =~ /$regexp/ ? 'Yes' : 'No', "\n"; } } print <<END; This is perl 5, version 38, subversion 0 (v5.38.0) built for x86_64-li +nux-thread-multi END

Outputs:

aabadcaa (?^:^((a*)b|a*b?d)*c\2$) No aababdcaa (?^:^((a*)b|a*b?d)*c\2$) No aabadcaa (?^:^(a*b?d|(a*)b)*c\2$) Yes aababdcaa (?^:^(a*b?d|(a*)b)*c\2$) Yes This is perl 5, version 38, subversion 0 (v5.38.0) built for x86_64-li +nux-thread-multi

Replies are listed 'Best First'.
Re^2: Precise backreference semantics in Perl regular expressions
by kikuchiyo (Hermit) on Nov 09, 2023 at 17:26 UTC
Re^2: Precise backreference semantics in Perl regular expressions
by tybalt89 (Monsignor) on Nov 10, 2023 at 04:13 UTC

    Opinion...

    #!/usr/bin/perl use strict; #use warnings; for my $regexp ( qr/^((a*)b|a*b?d)*c\2$/, qr/^(a*b?d|(a*)b)*c\2$/, qr/^((a*)b|a*b?d())*c\2$/, # NOTE these two fixed to work the way qr/^(?|a*b?d()|(a*)b)*c\1$/, # I think they should work ) { for my $string (qw( aabadcaa aababdcaa )) { print "\n$string\n$regexp\n"; print $string =~ /$regexp/ ? 'Yes' : 'No', "\n"; } } print <<END; This is perl 5, version 38, subversion 0 (v5.38.0) built for x86_64-li +nux-thread-multi END

    Outputs:

    aabadcaa (?^:^((a*)b|a*b?d)*c\2$) No aababdcaa (?^:^((a*)b|a*b?d)*c\2$) No aabadcaa (?^:^(a*b?d|(a*)b)*c\2$) Yes aababdcaa (?^:^(a*b?d|(a*)b)*c\2$) Yes aabadcaa (?^:^((a*)b|a*b?d())*c\2$) No aababdcaa (?^:^((a*)b|a*b?d())*c\2$) No aabadcaa (?^:^(?|a*b?d()|(a*)b)*c\1$) No aababdcaa (?^:^(?|a*b?d()|(a*)b)*c\1$) No This is perl 5, version 38, subversion 0 (v5.38.0) built for x86_64-li +nux-thread-multi