boost正则表达式

匹配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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;
}

查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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;
}