C++ 如何在boost::regex中找到子匹配项在字符串中的位置
在C++ 如何在boost::regex中找到子匹配项在字符串中的位置,c++,boost-regex,C++,Boost Regex,在boost::regex\u搜索(开始、结束、什么、模式)之后,我可以通过调用what.position()在搜索字符串中找到完全匹配的开始位置 我怎样才能找到潜艇的位置 我需要这样的代码: if(boost::regex_search(start, end, what, pat)) { int len = what["namedGroup"].length(); int pos = what["namedGroup"].position(); } 我找到了解决办法 在我的例
boost::regex\u搜索(开始、结束、什么、模式)
之后,我可以通过调用what.position()
在搜索字符串中找到完全匹配的开始位置
我怎样才能找到潜艇的位置
我需要这样的代码:
if(boost::regex_search(start, end, what, pat))
{
int len = what["namedGroup"].length();
int pos = what["namedGroup"].position();
}
我找到了解决办法
在我的例子中,std::string::const_迭代器的boost::match_results的每个元素都有属性first和second,它们相应地指向搜索字符串中此子匹配的开始迭代器和结束迭代器。因此,您可以使用迭代器或通过std::distance()将它们转换为索引
std::string::const_迭代器开始、结束;
匹配结果是什么;
start=searchString.begin();
end=searchString.end();
if(boost::regex_搜索(开始、结束、内容、模式))
{
std::string::const_iterator beg=what[“namedGroup”]。首先;
std::string::const_iterator end=what[“namedGroup”]。秒;
int beginIndex=std::距离(起点、终点);
}
std::string::const_iterator start, end;
boost::match_results<std::string::const_iterator> what;
start = searchString.begin();
end = searchString.end ();
if(boost::regex_search(start, end, what, pattern))
{
std::string::const_iterator beg = what["namedGroup"].first;
std::string::const_iterator end = what["namedGroup"].second;
int beginIndex = std::distance(start, beg);
}