簡體   English   中英

在boost regex中使用字符串迭代器而不是char *

[英]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*是雙向迭代器,所以你傳遞( strstr+strlen(str) )作為開始和結束),甚至有char *的重載,將其視為C字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM