简体   繁体   中英

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. I do not know how to use the std::string iterators over char*. So I created std::string from char* and using them. But now I want pointers in the original char* which can only be found using std::string I created. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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