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

Re: Precise backreference semantics in Perl regular expressions

by tybalt89 (Monsignor)
on Nov 09, 2023 at 17:11 UTC ( [id://11155516]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (7)
As of 2024-05-20 15:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found