C++ tokenize a string using a regular expression

一个人想着一个人 提交于 2019-11-29 01:57:26

The boost libraries are usually a good choice, in this case Boost.Regex. There even is an example for splitting a string into tokens that already does what you want. Basically it comes down to something like this:

boost::regex re("[\\sXY]+");
std::string s;

while (std::getline(std::cin, s)) {
  boost::sregex_token_iterator i(s.begin(), s.end(), re, -1);
  boost::sregex_token_iterator j;
  while (i != j) {
     std::cout << *i++ << " ";
  }
  std::cout << std::endl;
}

If you want to minimize use of iterators, and pithify your code, the following should work:

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

int main()
{
  const boost::regex re("[\\sXY,]+");

  for (std::string s; std::getline(std::cin, s); ) 
  {
    std::cout << regex_replace(s, re, " ") << std::endl;   
  }

}

Unlike in Perl, regular expressions are not "built in" into C++.

You need to use an external library, such as PCRE.

Regex are part of TR1 included in Visual C++ 2008 SP1 (including express edition) and G++ 4.3.

Header is <regex> and namespace std::tr1. Works great with STL.

Getting started with C++ TR1 regular expressions

Visual C++ Standard Library : TR1 Regular Expressions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!