繁体   English   中英

遍历Boost regex_iterator结果

[英]iterating over boost regex_iterator results

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

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

有谁知道我如何可以逐个遍历每个IP地址,而不必诉诸于这样的简单易用的workIterRegex。

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

我还在循环sregex_iterator结果中使用该示例来访问子匹配项,但是它给出了相似的结果。

使用workIterRegex后,代码每行打印出一个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})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM