gpt4 book ai didi

c++ - 遍历 boost regex_iterator 结果

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:24:34 27 4
gpt4 key购买 nike

我需要一些帮助来了解如何迭代 boost::sregex_iterator 的搜索结果。基本上我传递了一个';'从命令行分隔的一组 IP 地址,我希望能够使用 boost::sregex_iterator 依次处理每个 IP 地址。

下面的代码演示了我正在尝试做的事情,还展示了使用 workingIterRegex 的解决方法 - 但是该解决方法限制了我的正则表达式的丰富性。我尝试修改 nonworkingIterRegex 但它只将最后一个 IP 地址返回给 lambda。

有谁知道我如何可以单独遍历每个 IP 地址,而不必求助于这种被黑客攻击和简单化的 workingIterRegex。

我找到了以下 http://www.cs.ucr.edu/~cshelton/courses/cppsem/regex2.cc展示如何使用各个子匹配项调用 lambda。

我也使用了 looping through sregex_iterator results 中的示例访问子匹配但是它给出了类似的结果。

在使用 workingIterRegex 之后,代码每行打印出一个 IP 地址

#include <string>
#include <boost/regex.hpp>
...
std::string errorMsg;
std::string testStr("192.168.1.1;192.168.33.1;192.168.34.1;192.168.2.1");
static const boost::regex nonworkingIterregex (
"((\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3});?)+",
boost::regex_constants::icase);
boost::smatch match;
if (boost::regex_match(testStr, match, nonworkingIterregex)) {
static const boost::regex workingIterRegex("[^;]+");
std::for_each(boost::sregex_iterator(
begin(iter->second), end(iter->second), workingIterRegex),
boost::sregex_iterator(),
[](const boost::smatch &match){
std::cout << match.str() << std::endl;
}
);

mNICIPAddrs = UtlStringUtils::tokenize(iter->second, ";");
std::string errorMsg;
} else {
errorMsg = "Malformed CLI Arg:" + iter->second;
}
if (!errorMsg.empty()) {
throw std::invalid_argument(errorMsg);
}
...

经过一些实验,我发现以下方法有效 - 但我不确定为什么 c

最佳答案

尝试像这样使用您的正则表达式:

(\\d{1,3}\\.){3}(\\d{1,3})

关于c++ - 遍历 boost regex_iterator 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21266817/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com