【问题标题】:C++ tokenize a string using a regular expressionC++ 使用正则表达式标记字符串
【发布时间】:2010-11-02 19:01:45
【问题描述】:

目前我正在尝试从头开始学习一些 C++。
我精通 python、perl、javascript,但只在 过去的课堂设置。请原谅我的问题太天真了。

我想使用正则表达式拆分字符串,但没有找到太多运气 一个清晰、明确、高效且完整的示例,说明如何在 C++ 中执行此操作。

在 perl 中,这是一种常见的操作,因此可以以一种简单的方式完成,

/home/me$ cat test.txt
this is  aXstringYwith, some problems
and anotherXY line with   similar issues

/home/me$ cat test.txt | perl -e'
> while(<>){
>   my @toks = split(/[\sXY,]+/);
>   print join(" ",@toks)."\n";
> }'
this is a string with some problems
and another line with similar issues

我想知道如何最好地在 C++ 中完成等效操作。

编辑:
我想我在 boost 库中找到了我想要的东西,如下所述。

boost regex-token-iterator(为什么下划线不起作用?)

我想我不知道要搜索什么。


#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;
}

【问题讨论】:

  • 查看 Boost.Regex。我想你可以在这里找到答案:stackoverflow.com/questions/181624/…
  • 您应该添加“我自己找到”部分作为您自己问题的答案,而不是让它成为您问题的一部分......尽管提到您找到它并发布了答案。如果其他人发现这个问题很有用......他们会希望看到社区选择的答案以及您选择的答案。您的回答可能不是社区的最佳选择。

标签: c++ regex split tokenize


【解决方案1】:

boost 库通常是一个不错的选择,在本例中为 Boost.Regex。甚至还有an example 用于将字符串拆分为已经可以满足您要求的标记。基本上它归结为这样的:

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;
}

【讨论】:

  • 虽然我从 oberoi 的帖子中找到了自己的方式来使用 regex_token_iterator,但我选择这个作为答案是因为它提供了一个简洁、有效的示例,并包含指向适当提升页面的链接。干杯。
【解决方案2】:

如果你想尽量减少迭代器的使用,并精简你的代码,以下应该可以工作:

#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;   
  }

}

【讨论】:

    【解决方案3】:

    与 Perl 不同,正则表达式不是“内置”到 C++ 中的。

    您需要使用外部库,例如PCRE

    【讨论】:

    • 这是否也包含“拆分”功能? python 包含一个默认的正则表达式模块,'re',它提供了字符串分割的便利功能。我想知道这是否同样有效?
    • 这个答案在提交时是正确的,但随着 C++11 的可用性不再正确。 #include &lt;regex&gt;
    【解决方案4】:

    正则表达式是 TR1 的一部分,包含在 Visual C++ 2008 SP1(包括快速版)和 G++ 4.3 中。

    标题是&lt;regex&gt; 和命名空间std::tr1。与 STL 完美搭配。

    Getting started with C++ TR1 regular expressions

    Visual C++ Standard Library : TR1 Regular Expressions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-13
      • 2012-01-16
      • 1970-01-01
      • 2015-05-26
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多