N
N
Nikita Gusakov2014-06-21 16:35:12
C++ / C#
Nikita Gusakov, 2014-06-21 16:35:12

Why don't regex work?

Is there a simple code why std::regex_search returns true, but the matches themselves are not displayed?

#include <iostream>
#include <regex>

int main(int argc, const char * argv[])
{
    std::string strs[] = {
        "/users/{id}",
        "/users/{i:id}"
    };
    std::regex pattern("{(?:(\\w+):)*(\\w+)}");
    std::smatch matches;
    
    for (auto str : strs) {
        std::cout << "String: " << str << std::endl;
        
        std::cout << "Match: " << std::boolalpha << std::regex_search(str, matches, pattern) << std::endl;
        
        for (auto match : matches) {
            std::cout << match.str() << std::endl;
        }
    }
}

String: /users/{id}
Match: true

String: /users/{i:id}
Match: true

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sumor, 2014-06-21
@hell0w0rd

The { and } characters are service characters and must be escaped.
std::regex pattern("[{](?:(\\w+):)*(\\w+)[}]");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question