develooper Front page | perl.perl5.porters | Postings from August 2001

[ID 20010803.016] Possible regex bug in 5.6.1

Thread Next
From:
Philip Hazel
Date:
August 3, 2001 06:53
Subject:
[ID 20010803.016] Possible regex bug in 5.6.1
Message ID:
Pine.SOL.4.33.0108031439180.981-100000@virgo.cus.cam.ac.uk
Hello. This proglet:

-----------------------------------------------------------------------
print "Running Perl $]\n\n";

print "\"ABABAB\" =~ /(AB)*?\\1/\n";

if ("ABABAB" =~ /(AB)*?\1/)
  {
  print "Match\n";
  print "\$1 is defined as \"$1\"\n" if (defined $1);
  }
else
  {
  print "No match\n";
  }
-----------------------------------------------------------------------

produces this output:

-----------------------------------------------------------------------
Running Perl 5.006001

"ABABAB" =~ /(AB)*?\1/
Match
$1 is defined as ""
-----------------------------------------------------------------------

Is this right? The (AB)*? has matched zero times, but the \1 is treating
this as if the subpattern had matched an empty string, but in fact it
hasn't matched at all.

Consider the case of /(AB)|(CD)/ matching against "CD". In this case, $1
is not defined after the successful match.

This is a subtle area, but I'm not sure that these two cases should be
different. Certainly it feels wrong that the contents of the parens (AB)
are ever reported as having matched anything other than "AB"...

Regards,
Philip



Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About