c++ - Regex and Boost. Not working on a simple regex -
below following code
#include <iostream> #include <stdlib.h> #include <boost/regex.hpp> #include <string> using namespace std; using namespace boost; int main() { std::string s = "hello name bob"; boost::regex re("name"); boost::cmatch matches; try{ // if (boost::regex_match(s.begin(), s.end(), re)) if (boost::regex_match(s.c_str(), matches, re)){ cout << matches.size(); // matches[0] contains original string. matches[n] // contains sub_match object each matching // subexpression (int = 1; < matches.size(); i++){ // sub_match::first , sub_match::second iterators // refer first , 1 past last chars of // matching subexpression string match(matches[i].first, matches[i].second); cout << "\tmatches[" << << "] = " << match << endl; } } else{ cout << "no matches(" << matches.size() << ")\n"; } } catch (boost::regex_error& e){ cout << "error: " << e.what() << "\n"; } }
it outputs no matches.
im sure regex should work.
i used example
http://onlamp.com/pub/a/onlamp/2006/04/06/boostregex.html?page=3
from boost regex:
important
note result true if expression matches whole of input sequence. if want search expression somewhere within sequence use regex_search. if want match prefix of character string use regex_search flag match_continuous set.
Comments
Post a Comment