[英]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.