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>: Incorrect behavior for capture groups #731

Open
stevemk14ebr opened this issue Apr 20, 2020 · 2 comments · May be fixed by #5218
Open

<regex>: Incorrect behavior for capture groups #731

stevemk14ebr opened this issue Apr 20, 2020 · 2 comments · May be fixed by #5218
Labels
bug Something isn't working

Comments

@stevemk14ebr
Copy link

stevemk14ebr commented Apr 20, 2020

Describe the bug
In the following code snippet, the second regex capture called callConv fails to be captured. Instead the group is set to 'false'. This behavior does not occur on clang or GCC.

#include <iostream>
#include <regex>

int main()
{
    std::regex fnTypeDefRgx("([a-zA-Z_][a-zA-Z0-9_*]*)\\s*(?:([a-zA-Z_][a-zA-Z0-9_*]*)?\\s*)?(?:[a-zA-Z_][a-zA-Z0-9_]*)?\\s*\\((.*)\\)");
    std::smatch matches;
    std::string input = "void stdcall (char*, float, uint8_t)";
    if (std::regex_search(input, matches, fnTypeDefRgx)) {
        std::string retType = matches[1].str();
        std::string callConv = matches[2].str(); // this guy is 'false' BUG
        std::cout << callConv << std::endl;
    }
    return 0;
}

for convenience, the unespaced regex is as follows. It regexs the return value, optional calling convention, and then parameters all as one with optional *'s on the type names.

([a-zA-Z_][a-zA-Z0-9_*]*)\s*(?:([a-zA-Z_][a-zA-Z0-9_*]*)?\s*)?(?:[a-zA-Z_][a-zA-Z0-9_]*)?\s*\((.*)\)

Expected behavior
The expected output would be that the second capture group contains the string 'stdcall' as in the online compiler here: https://onlinegdb.com/ry28UXodI

STL version
Microsoft Visual Studio Community 2019 Version 16.4.5

@stevemk14ebr
Copy link
Author

I debugged this some more, the issue appears to be capture groups inside of non-capturing groups. When re-written with the equivalent expression, the results are as expected:

([a-zA-Z_][a-zA-Z0-9_*]*)\\s*([a-zA-Z_][a-zA-Z0-9_*]*\\s*)?(?:[a-zA-Z_][a-zA-Z0-9_]*)?\\s*\\((.*)\\)

This confirms incorrect behavior I believe. When testing with any other regex engine the first variant captures correctl.

@StephanTLavavej StephanTLavavej added the bug Something isn't working label Apr 21, 2020
@StephanTLavavej StephanTLavavej changed the title MSVC Regex fails to capture group <regex>: Incorrect behavior for capture groups Apr 21, 2020
@StephanTLavavej
Copy link
Member

Reduced test case; I was able to eliminate the non-capture groups:

C:\Temp>type meow.cpp
#include <iostream>
#include <regex>
#include <string>
using namespace std;

int main() {
    const regex r{R"((A+)\s*(B+)?\s*B*)"};
    smatch m;
    const string s{"AAA BBB"};
    if (regex_match(s, m, r)) {
        cout << "This should be \"AAA\": \"" << m[1] << "\"\n";
        cout << "This should be \"BBB\": \"" << m[2] << "\"\n";
    }
}

C:\Temp>cl /EHsc /nologo /W4 meow.cpp
meow.cpp

C:\Temp>meow
This should be "AAA": "AAA"
This should be "BBB": ""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants