【问题标题】:Is there a way to get the PREMATCH ($`) and POSTMATCH ($') from pcrecpp?有没有办法从 pcrecpp 获取 PREMATCH ($`) 和 POSTMATCH ($')?
【发布时间】:2010-03-14 17:27:54
【问题描述】:

有没有办法从 pcrecpp 获得 Perl 的 PREMATCH ($`) 和 POSTMATCH ($') 的 C++ 等价物?我会很高兴有一个字符串、一个 char * 或指向此点的索引/startpos+length 对。

StringPiece 似乎可以完成其中的一部分,但我不确定如何获得它。

在 perl 中:

$_ = "Hello world";
if (/lo\s/) {
    $pre = $`; #should be "Hel"
    $post = $'; #should be "world"
}

在 C++ 中我会有类似的东西:

string mystr = "Hello world"; //do I need to map this in a StringPiece?
if (pcrecpp::RE("lo\s").PartialMatch(mystr)) { //should I use Consume or FindAndConsume?
   //What should I do here to get pre+post matches???
}

pcre plainjane c 似乎有能力返回匹配的向量,包括字符串的“结束”部分,所以理论上我可以提取这样的前/后变量,但这似乎需要做很多工作。我喜欢 pcrecpp 界面的简洁性。

建议?谢谢!

--艾瑞克

【问题讨论】:

    标签: c++ regex perl pcre


    【解决方案1】:

    您可以使用 FullMatch 代替 PartialMatch 并明确捕获 pre 和 post 自己,例如

    string pre, match, post;
    RE("(.*)(lo\\s)(.*)").FullMatch("Hello world", &pre, &match, &post);
    

    【讨论】:

      猜你喜欢
      • 2018-09-25
      • 2021-12-17
      • 2014-05-30
      • 2020-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-02
      • 2020-02-29
      相关资源
      最近更新 更多