Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Behavior of isolated capture groups with alternation #89

Closed
Thom1729 opened this issue Jun 11, 2018 · 1 comment
Closed

Behavior of isolated capture groups with alternation #89

Thom1729 opened this issue Jun 11, 2018 · 1 comment

Comments

@Thom1729
Copy link

The following two regexps seem to be equivalent:

a(?i)b|c
a(?i:b|c)

I expected the following to be equivalent instead:

a(?i)b|c
(?:a(?i)b)|c

The documentation mentions something like this, but it isn't completely clear to me. Is this behavior correct? If so, it might be helpful to add a test somewhere around here.

@kkos
Copy link
Owner

kkos commented Jun 12, 2018

/a(?i)b|c/ is interpreted as /a(?i)(?:b|c)/ or /a(?i:b|c)/.
It is intended to match with Perl 5.

#!/usr/bin/perl

if ("C" =~ /(?i)b|c/) {
    print "OK\n";
}
else {
    print "FAIL\n";
}

if ("aC" =~ /a(?i)(?:b|c)/) {
    print "OK\n";
}
else {
    print "FAIL\n";
}

if ("C" =~ /(?:a(?i)b)|c/) {
    print "OK\n";
}
else {
    print "FAIL\n";
}

OK
OK
FAIL

But I just found /a(?i)b|c/ is not interpreted as /a(?i)(?:b|c)/ in Perl 5.

if ("C" =~ /a(?i)b|c/) {
    print "OK\n";
}
else {
    print "FAIL\n";
}

OK

There are different scopes for alternatives and option.
It is strange behavior for me, and I don't want to follow it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants