[英]How to tokenize and transform a c-string using regex and boost transform iterators?
[英]using string iterators over char* in boost regex
我正在尝试搜索char *以查找匹配并使用boost regex将每个匹配存储为结构。 我不知道如何在char *上使用std :: string迭代器。 所以我从char *创建了std :: string并使用它们。 但现在我想要原始char *中的指针,只能使用我创建的std :: string找到它。 请参阅以下代码。 评论应该清除你的疑虑。
typedef struct {
void *pFind; // Pointer to begining of match
int lenFind; // Length of match
} IppRegExpFind;
bool regExSearch(int nStrLen /*Length of input string*/,
std::vector<IppRegExpFind>& vectResult /*vector of struct to store matches*/,
int &numMatches /* number of matches*/,
const char *sStrInput /*Input string to be searched*/,
const char *sStrRegex /*Input regex string*/)
{
bool bStatusOk = true;
try{
std::string regexStr(sStrRegex);
std::string inputStr(sStrInput);
static const boost::regex ex(regexStr);
std::string::const_iterator start, end;
start = inputStr.begin();
end = inputStr.end();
boost::match_results<std::string::const_iterator> what;
boost::match_flag_type flags = boost::match_default;
std::vector <std::string> matches;
while(boost::regex_search(start, end, what, ex, flags))
{
matches.push_back(what.str());
start = what[0].second;
}
// convert boost:match_Results to a vector of IppRegExpFind
}
catch(...){
bStatusOk = false;
}
return bStatusOk;
}
您可以通过获取原始指针
sStrInput+what.position(0)
但是,我不确定为什么你需要使用std :: string的所有技巧。 根据文档, boost::regex_search
可以搜索双向迭代器指定的任何范围(即char*
是双向迭代器,所以你传递( str
, str+strlen(str)
)作为开始和结束),甚至有char *的重载,将其视为C字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.