boost::Regex的问题.....
- C/C++ code
#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; }以上代码为何只能输出第一个中文字符串:"北京市朝阳区";
第二个为什么查找不出来?
难道是查找到一个就停止吗,那如何才能使其继续查找?
[解决办法]
- C/C++ code
#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; }
[解决办法]
- C/C++ code
#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; }