我正在尝试搜索 char* 以查找匹配项并将每个匹配项存储为使用 boost 正则表达式的结构。我不知道如何在 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;
}