C++ CPP&x2B;验证URL的正则表达式
我想用c++{MFC}构建一个正则表达式来验证URL 正则表达式必须满足以下条件 有效URL:- http://www.google.com 无效的URL:-C++ CPP&x2B;验证URL的正则表达式,c++,c,regex,mfc,visual-c++,C++,C,Regex,Mfc,Visual C++,我想用c++{MFC}构建一个正则表达式来验证URL 正则表达式必须满足以下条件 有效URL:- http://www.google.com 无效的URL:- \MyWebSite/\ISAPIWEBSITE/\Denypage.aspx/=Regx必须检查“/\MyWebSite/\ISAPIWEBSITE/\Denypage.aspx/”之间的无效URL作为“\”字符(&F) =Regx必须检查URL并使其无效,因为URL中有多个“//”条目 =正则表达式必须检查URL并使其无效,以便额外
它表示使用^([^:/?#]+):(/([^/?#]*)?([^?#]*))([^?#]*)(\?([^?#]*))(#(.*))?
可以作为子匹配获得:
scheme = $2
authority = $4
path = $5
query = $7
fragment = $9
例如:
int main(int argc, char *argv[])
{
std::string url (argv[1]);
unsigned counter = 0;
std::regex url_regex (
R"(^(([^:\/?#]+):)?(//([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?)",
std::regex::extended
);
std::smatch url_match_result;
std::cout << "Checking: " << url << std::endl;
if (std::regex_match(url, url_match_result, url_regex)) {
for (const auto& res : url_match_result) {
std::cout << counter++ << ": " << res << std::endl;
}
} else {
std::cerr << "Malformed url." << std::endl;
}
return EXIT_SUCCESS;
}
这真的很棒。您能告诉我如何使用正则表达式提取字符串中所有匹配的URL吗?我尝试将它与sregex_迭代器一起使用,但没有找到任何匹配项。非常感谢你!不幸的是,这不是为了验证,而是为了将一个正确的URI拆分为多个部分。它甚至检测不到最简单的情况,比如未编码的空间。谢谢你提供了这样一个有用且解释得很好的答案。这是我发现的最全面的URL解析脚本,它具有准确性、易用性和快速实现性。您不需要下载任何特殊的库!这将很好地回答这个问题:请注意,如果存在端口,它将包含在元素3和4中。即,
http://localhost.com:8888/path?hue=br#cool
导致3://localhost.com:8888
和4:localhost.com:8888
。如果不起作用,此表达式将接受“aaa”
./url-matcher http://localhost.com/path\?hue\=br\#cool
Checking: http://localhost.com/path?hue=br#cool
0: http://localhost.com/path?hue=br#cool
1: http:
2: http
3: //localhost.com
4: localhost.com
5: /path
6: ?hue=br
7: hue=br
8: #cool
9: cool