#include #include #include #include // g++ -o regex-bug{,.cpp} -lboost_regex -Wall -pedantic // // tests case : // echo " toto" | regex '[a-z]*' > result_0 // echo " toto" | regex ' [a-z]*' > result_1 // // // char const * c_decorate_string = "% -12s (%s)" ; char const * c_decorate_matched = " [% -3d,% -3d] (%s)" ; int main(int argc, char * argv[]) { using namespace std ; using namespace boost ; cout << format(c_decorate_string) % "seeked" % argv[1] << endl ; regex re(argv[1]) ; string extract ; getline(cin, extract) ; cout << format(c_decorate_string) % "input" % extract << endl ; if(extract.length() > 0) { string const & c_extract = extract ; string::const_iterator start = c_extract.begin() ; string::const_iterator end = c_extract.end() ; smatch matches ; match_flag_type flags = match_default ; unsigned counter = 0 ; while(regex_search(start, end, matches, re, flags)) { cout << format(c_decorate_string) % "current" % std::string(start, end) << endl ; for(unsigned i = 0 ; i < matches.size() ; i++) cout << format(c_decorate_matched) % counter % i % std::string(matches[i].first, matches[i].second) << endl ; cout << format(c_decorate_string) % "suffix" % std::string(matches.suffix().first, matches.suffix().second) << endl ; cout << format(c_decorate_string) % "prefix" % std::string(matches.prefix().first, matches.prefix().second) << endl ; counter++ ; flags |= match_prev_avail ; flags |= match_not_bob ; if(matches[0].first == matches[0].second) throw "If the string is null, it means regex_search has to be false !" ; else start = matches[0].second ; } } return 0 ; }