1

我正在尝试搜索 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;
}
4

1 回答 1

4

您可以通过

sStrInput+what.position(0)

但是,我不确定为什么你需要 std::string 的所有技巧。根据文档,boost::regex_search可以搜索由双向迭代器指定的任何范围(即,char*是双向迭代器,因此您将 ( str, str+strlen(str)) 作为开始和结束传递),甚至 char* 的重载将其视为 C 字符串。

于 2009-02-17T02:14:47.837 回答