boost::Regex的问题.....
#include <iostream> #include <boost/regex.hpp> int main( int argc, char* argv[] ){ char *buf="c.addCity(\"4476\",\"北京市朝阳区\")cur.addBoard(new Board(\"CN\",\"3478\",\"浙江\")"; boost::regex exampleregex("(\"[^0-9a-zA-Z,]*\")"); boost::cmatch result; if(boost::regex_search( buf, result, exampleregex )) { std::cout << result << std::endl; } return 0; }#include <iostream> #include <boost/regex.hpp> int main( int argc, char* argv[] ){ char *buf="c.addCity(\"4476\",\"北京市朝阳区\")cur.addBoard(new Board(\"CN\",\"3478\",\"浙江\")"; boost::regex exampleregex(".*(?:,|\\()(\"[^0-9a-zA-Z,]*\")\\).*(?:,|\\()(\"[^0-9a-zA-Z,]*\")\\)"); boost::cmatch result; if(boost::regex_match( buf, result, exampleregex )) { for (int i = 0; i < result.size(); ++i) std::cout << result[i].str() << std::endl; } return 0; }
[解决办法]
#include <iostream> #include <boost/regex.hpp> #include <string>using namespace std;int main( int argc, char* argv[] ){ string buf="c.addCity(\"4476\",\"北京市朝阳区\")cur.addBoard(new Board(\"CN\",\"3478\",\"浙江\")"; boost::regex exampleregex("\"[^0-9a-zA-Z,]*\""); boost::cmatch result; string::const_iterator begin = buf.begin(); string::const_iterator end = buf.end(); while (boost::regex_search(begin, end, result, exampleregex )) { cout << string(result[0].first, result[0].second) << endl; begin = result[0].second; } return 0; }