且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C ++ 11中使用正则表达式匹配多个模式

更新时间:2023-09-01 14:23:04

尝试仅使用覆盖所有变体的单个交替.在下面的模式中,我还交替关闭捕获.这给我们留下了相当简单的匹配逻辑.如果 smatch 结果没有有一个条目,则它应该是具有完整匹配路径的单个条目.否则,它应该为空.

Try just using a single alternation which covers all variations. In the pattern below, I also turn off capturing in the alternation. This leaves us with fairly straightforward matching logic. If the smatch result does have an entry, then it should be a single entry with the entire matching path. Otherwise, it should be empty.

std::string regexString="/api/(?:Attachment|Attachment/upload|Attachment/download|v1/ApiTest|v1/ApiTest/get/[^/]*/[^/]*|v1/ApiTest/[^/]*/List)";
std::string s ("/api/Attachment/upload");
std::regex e (regexString);

std::smatch sm;
std::regex_match (s,sm,e);

if (sm.size() > 0) {
    std::cout << "found a matching path: " << sm[0];
}

found a matching path: /api/Attachment/upload