![](/img/trans.png)
[英]How to check if a string has a valid file path or directory path format in unmanaged C++ code?
[英]How to check a specified string is a valid URL or not using C++ code
没有任何可能的方法来检查指定的字符串是否是有效的url。 该解决方案必须使用c ++,并且无需互联网即可使用。
示例字符串是
good.morning
foo.goo.koo
https://hhhh
hdajdklbcbdhd
8881424.www.hfbn55.co.in/sdfsnhjk
://dgdh24.vom
dfgdfgdf(2001)/.com/sdgsgh
\adiihsdfghnhg.co.inskdhhj
aser//www.gtyuh.co.uk/kdsfgdfgfrgj
选择一个类似/^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([az\\.]{2,6})([\\/\\w \\.-]*)*\\/?$/
。
如果您没有C ++ 11,请使用std regex或boost regex :
if (std::regex_match ("http://subject", std::regex("^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$") )) {
// ...
}
您可以使用regex 。 什么是正则表达式。
使用C ++ 11,regex内置在STD库regex c ++ 11中 。
如果由于某种原因不能使用C ++ 11,则可以使用boost库。
无论如何,您可以使用以下方法检查网址的模式:
#include <regex> //require c++11
// ...
// regex pattern
std::string pattern = "https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)";
// Construct regex object
std::regex url_regex(pattern);
// An url-string for example
std::string my_url = "http://www.google.com/img.png";
// Check for match
if (std::regex_match(my_url, url_regex) == true) {
std::cout << "This is a well-formed url\n";
} else {
std::cout << "Ill-formed url\n";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.