简体   繁体   English

PCRE忽略C ++中的匹配项

[英]PCRE ignoring matches in c++

I'm trying to work with C++ and PCRE regular expressions in Ubuntu. 我正在尝试在Ubuntu中使用C ++和PCRE正则表达式。 I installed almost every piece of software related (libpcrepp and similar), but I can't even match the simplest expression. 我安装了几乎所有与软件相关的软件(libpcrepp和类似软件),但我什至无法匹配最简单的表达式。 My code, simplified: 我的代码简化了:

#include <iostream>
#include <string>
#include <pcrecpp.h>

using namespace std;

int main() {

   std::string text, a, b;

   text = "Flowers in the forest are darker than in the prairie";

   pcrecpp::RE re("forest");

   if( re.PartialMatch(text, &a, &b) ) {
      std::cout << "match: " << a << b << "\n";
   }

}

No errors compiling: 编译没有错误:

g++ t2.cpp -lpcrecpp -o t2

And no results when executing. 而且执行时没有结果。 Any hint? 有什么提示吗? Thanks in advance. 提前致谢。

re.PartialMatch(text, &a, &b) re.PartialMatch(文本,&a,&b)

can only return true if there are at least two captures in the regular expression, one for each of the return arguments. 如果正则表达式中至少有两个捕获,每个返回参数一个,则只能返回true。 Since there are no captures in your regular expression ("forest"), re.PartialMatch is guaranteed to return false, regardless of whether the pattern matches the text. 由于您的正则表达式(“林”)中没有捕获,因此无论模式是否与文本匹配,都保证re.PartialMatch返回false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM