匹配
int TestRegexMatch()
{
try
{
std::string a{ "http://192.1168.1.1:65535/123" };
boost::regex reg{ "^((http|https)://)?([\\w-]+\\.)+[\\w-]+(:([0-9]|[1-9]\\d|[1-9]\\d{2}|[1-9]\\d{3}|[1-5]\\d{4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5]))?(/[\\w./?%&=-]*)?$" };
bool r = boost::regex_match(a.c_str(), reg);
if (r)
{
printf("success");
}
else
{
printf("failure");
}
}
catch (const std::exception& e)
{
printf("%s", e.what());
}
return 0;
}
查找
int TestRegexSearch()
{
try
{
std::string a{ "http://192.1168.1.1:65535/123" };
boost::cmatch mat;
boost::regex reg{ "^((http|https)://)?([\\w-]+\\.)+[\\w-]+(:([0-9]|[1-9]\\d|[1-9]\\d{2}|[1-9]\\d{3}|[1-5]\\d{4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5]))?(/[\\w./?%&=-]*)?$" };
bool r = boost::regex_search(a.c_str(), mat, reg);
if (r)
{
a = mat[0];
printf("find: %s\n", a.c_str());
}
}
catch (const std::exception& e)
{
printf("%s", e.what());
}
return 0;
}
版权声明: 本博客所有文章除特别声明外,均采用CC BY-NC-SA 4.0 许可协议。转载请注明来自 张拓的博客!