如何解决如何在std :: sub_match中访问正则表达式搜索结果?
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
内的内部字符串,怎么办?
解决方法
您的问题与正则表达式无关,但字符串转换为int。
您可以使用std::atoi
/ std::stoi
:
uint8_t a = std::stoi(ipv4Match[1].str());
uint8_t b = std::stoi(ipv4Match[2].str());
uint8_t c = std::stoi(ipv4Match[3].str());
uint8_t d = std::stoi(ipv4Match[4].str());
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。