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

<regex>: Successful negative lookahead assertions sometimes mistakenly assign matches to capture groups #5245

Open
muellerj2 opened this issue Jan 18, 2025 · 0 comments · May be fixed by #5269
Labels
bug Something isn't working regex Everyone's favorite header

Comments

@muellerj2
Copy link
Contributor

When a negative lookahead assertion is successful (i.e., the asserted regular expression does not match), some capture groups might be assigned in the asserted regular expression as if the lookahead regular expression had matched.

Test case

#include <iostream>
#include <regex>

using namespace std;

int main() {
    const regex re("(?!(a)b)..");
    cmatch match;

    cout << "'(?!(a)b)..' matches 'ac': "
         << regex_match("ac", match, re)
         << '\n';
    cout << "capture group 1 matched: "
         << match[1].matched
         << '\n';
}

https://godbolt.org/z/EzjGrGP5K

This prints:

'(?!(a)b)..' matches 'ac': 1
capture group 1 matched: 1

Expected result

This should print:

'(?!(a)b)..' matches 'ac': 1
capture group 1 matched: 0

It follows from ECMA-262 3rd ed., Section 15.10.2.8 "Atom", that capture groups in negative lookahead assertions are never matched, whether the asserted regular expression matches or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working regex Everyone's favorite header
Projects
None yet
3 participants