简体   繁体   English

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

[英]using string iterators over char* in boost regex

I am trying to search a char* to find matches and store each match as a struct using boost regex. 我正在尝试搜索char *以查找匹配并使用boost regex将每个匹配存储为结构。 I do not know how to use the std::string iterators over char*. 我不知道如何在char *上使用std :: string迭代器。 So I created std::string from char* and using them. 所以我从char *创建了std :: string并使用它们。 But now I want pointers in the original char* which can only be found using std::string I created. 但现在我想要原始char *中的指针,只能使用我创建的std :: string找到它。 See the following code. 请参阅以下代码。 The comments should clear your doubts. 评论应该清除你的疑虑。

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;
}

You can get the original pointer by 您可以通过获取原始指针

sStrInput+what.position(0)

I'm not sure, however, why do you need all the tricks with std::string. 但是,我不确定为什么你需要使用std :: string的所有技巧。 According to the documentation, boost::regex_search can search any range specified by bidirectional iterators (ie. char* is a bidirectional iterator, so you pass ( str , str+strlen(str) ) as start and end), and there are even overloads for char* that treat it as C string. 根据文档, boost::regex_search可以搜索双向迭代器指定的任何范围(即char*是双向迭代器,所以你传递( strstr+strlen(str) )作为开始和结束),甚至有char *的重载,将其视为C字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM