【发布时间】:2020-08-25 03:29:15
【问题描述】:
std::smatch ipv4Match;
std::regex_match(ipv4, ipv4Match, ip);
if (ipv4Match.empty())
{
return std::nullopt;
}
else
{
if (!ipv4Match.empty())
{
uint8_t a, b, c, d;
a = (uint8_t)(ipv4Match[0]);
b = (uint8_t)(ipv4Match[1]);
c = (uint8_t)(ipv4Match[2]);
d = (uint8_t)(ipv4Match[3]);
}
}
但它显然不起作用。我研究过,当我使用[] 访问smatch 时,它返回一个sub_match,它没有公共成员,除非构造函数。
如何将ip地址匹配的每一部分转换成一个字节?
最重要的是,如果std::cout << ipv4Match[0] 无法访问ipv4Match 内的内部字符串,因为它是sub_match,它如何工作?
【问题讨论】:
-
@DmitryKuzminov 我留下了第二个 if 因为我稍后也会添加 ipv6 支持。所以,en.cppreference.com/w/cpp/regex/match_results/operator_at 返回 `std::sub_match`
-
std::sub_match具有length、str、operator string_type和compare公共方法。 en.cppreference.com/w/cpp/regex/sub_match