boost 正则 分割字符串

参考链接

    正则30分钟

    boost_regex

boost.cc

#include <iostream>
#include <boost/regex.hpp>

using namespace std;

int main(int argc)
{
   string s;
   do{
      if(argc == 1)
      {
         cout << "Enter text to split (or \"quit\" to exit): ";
         getline(cin, s);
         if(s == "quit") break;
      }
      else
         s = "This is a string of tokens";

      boost::regex re("\\s+");
      boost::sregex_token_iterator i(s.begin(), s.end(), re, -1);
      boost::sregex_token_iterator j;

      unsigned count = 0;
      while(i != j)
      {
         cout << *i++ << endl;
         count++;
      }
      cout << "There were " << count << " tokens found." << endl;

   }while(argc == 1);
   return 0;
}

g++ -g boost.cc -o boost1 -lboost_regex

Enter text to split (or "quit" to exit): wang kang luo1 bt   (备注 空格数量不等)
wang
kang
luo1
bt
There were 4 tokens found.

原文地址:https://www.cnblogs.com/wangkangluo1/p/2537941.html