regex bug?

E

Eric Niebler

Consider the following program:

$str = 'aaA';
$str =~ /(((?:a))?)+/i;
if(defined($2)) { print "$2"; }
else { print "not defined"; }

This prints "not defined," and I think that's right. But if I change the
regex to /(((a))?)+/i (that is, if I change the third group from
non-capturing to capturing), the program prints "A".

I can't think of a reason why changing group 3 from non-capturing to
capturing should have any effect on whether group 2 captures anything.
Seems like a regex bug to me. Opinions?

FWIW, I'm running ActiveState perl v5.8.7 815 for Win32.

Thanks,
Eric
 
R

robic0

Consider the following program:

$str = 'aaA';
$str =~ /(((?:a))?)+/i;
^
? = zero or one. it found nothing first,
it always finds the first match then looks for others
ie "not defined"

No bug..
 
R

robic0

^
? = zero or one. it found nothing first,
it always finds the first match then looks for others
ie "not defined"

No bug..

Oh, capture was turned off thats why it didn't find
anything. Didn't specify anything else to match outside the
non-capture. It matched but it didn't capture.
 
R

robic0

Oh, capture was turned off thats why it didn't find
anything. Didn't specify anything else to match outside the
non-capture. It matched but it didn't capture.

Yep, the no capture just stops $3, $1 and $2 should have picked it up.
The real problem stems fro (?:..) which is experimental.
If that is experimental, what do you expect from it?
Why do you get A from /((((((((((a)))))))))))?/+++++/i anyway?
shouldn't you get 'a' from that!!!!!!!?????????????
 
J

John W. Krahn

Eric said:
Consider the following program:

$str = 'aaA';
$str =~ /(((?:a))?)+/i;
if(defined($2)) { print "$2"; }
else { print "not defined"; }

This prints "not defined," and I think that's right. But if I change the
regex to /(((a))?)+/i (that is, if I change the third group from
non-capturing to capturing), the program prints "A".

I can't think of a reason why changing group 3 from non-capturing to
capturing should have any effect on whether group 2 captures anything.
Seems like a regex bug to me. Opinions?

FWIW, I'm running ActiveState perl v5.8.7 815 for Win32.

When using (((a))?)+ $3 is not optional so it must capture something and since
it is inside $2 then $2 will also capture the same thing. When using
(((?:a))?)+ it is the same as saying ((a)?)+ where the contents of $2 are
optional.


John
 
A

Anno Siegel

John W. Krahn said:
When using (((a))?)+ $3 is not optional so it must capture something and since
it is inside $2 then $2 will also capture the same thing. When using
(((?:a))?)+ it is the same as saying ((a)?)+ where the contents of $2 are
optional.

I don't buy that explanation. $3 is in the range of a "?", so it
*is* optional. (Actually, the *match* of this part is optional, the
capture is never optional.) Witness

use Data::Dumper;

my $str = 'bbB';
$str =~ /(((a))?)+/i or die;
print Dumper [ $1, $2, $3];

which doesn't die.

I think it's a bug.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top