2009-02-17 9 views
1

J'essaye de chercher un char * pour trouver des correspondances et stocker chaque match comme une structure en utilisant boost regex. Je ne sais pas comment utiliser les itérateurs std :: string sur char *. J'ai donc créé std :: string à partir de char * et en les utilisant. Mais maintenant je veux des pointeurs dans le char original * qui ne peut être trouvé qu'en utilisant std :: string que j'ai créé. Voir le code suivant. Les commentaires devraient effacer vos doutes.en utilisant des itérateurs de chaînes sur char * dans boost regex

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

Répondre

4

Vous pouvez obtenir le pointeur d'origine par

sStrInput+what.position(0) 

Je ne suis pas sûr, cependant, pourquoi avez-vous besoin tous les trucs avec std :: string. Selon la documentation, boost::regex_search peut rechercher n'importe quelle plage spécifiée par des itérateurs bidirectionnels (ie char* est un itérateur bidirectionnel, donc vous passez (str, str+strlen(str)) comme début et fin), et il y a même des surcharges pour char * qui le traitent comme C chaîne.

Questions connexes