Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Precise backreference semantics in Perl regular expressions

by choroba (Cardinal)
on Nov 09, 2023 at 15:21 UTC ( [id://11155510]=note: print w/replies, xml ) Need Help??


in reply to Precise backreference semantics in Perl regular expressions

Seems weird. And what really smells suspicious is that if you switch the order of the alternatives, the second string suddenly matches the regex.
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 )) { say $string =~ /$regexp/ ? 'Yes' : 'No'; } }
Output:
Yes No Yes Yes

If you want to know what Perl's regex engine does, not just guess, run the script with

use re 'debug';

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-05-29 04:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found