Skip to content

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

Description

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedSomething works now, yay!regexmeow is a substring of homeowner

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions