【问题标题】:Preg match all in pcre c++Preg匹配所有在pcre c ++中
【发布时间】:2016-04-12 05:40:06
【问题描述】:

你好,这是我的字符串

last_name, first_name
bjorge, philip
kardashian, kim
mercury, freddie

php 我正在使用preg_match_all (pcre) 启动regex 进程

preg_match_all("/(.*), (.*)/", $input_lines, $output_array);

现在我在 c++ 上安装了 pcre,我想知道 c++ pcre 中与我的 php 代码相等的具体过程是什么?像 php preg_match_all 一样工作的 c++ pcre 中究竟有什么功能?

【问题讨论】:

标签: c++ pcre pcregrep


【解决方案1】:

在 C++ 11 中,标准库支持正则表达式。所以没有任何具体原因你不需要使用pcre。

例如上面的例子,你可以使用标准的正则表达式来达到同样的效果。例如:

#include <iostream>
#include <string>
#include <regex>

int main()
{
  std::vector<std::string> input = {
    "last_name, first_name",
    "bjorge, philip",
    "kardashian, kim",
    "mercury, freddie"
  };

  std::regex re("(.*), (.*)");
  std::smatch pieces;

  for (const std::string &s : input) {
    if (std::regex_match(s, pieces, re)) {
      std::cout << "Pieces: " << pieces.size() << std::endl;
      for (size_t i = 0; i < pieces.size(); ++i) {
        std::cout << pieces[i].str() << std::endl;
      }
    }
  }

  return 0;
}

【讨论】:

  • 我知道并尝试了。但我认为 pcre 更快,我想使用它。我已经使用默认的正则表达式 ....
  • 只是好奇,快多少?
  • 这么快,我想用(在繁重的过程中)编写我的项目
  • 是的,找到这个链接boost.org/doc/libs/1_60_0/libs/regex/doc/html/boost_regex/…std::regex 还需要改进:)
猜你喜欢
  • 2011-12-08
  • 2010-12-28
  • 1970-01-01
  • 2015-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多