c++ - 有没有办法从 pcrecpp 获取 PREMATCH ( $`) and POSTMATCH ($' )?

标签 c++ regex perl pcre

有没有办法从 pcrecpp 获得 Perl 的 PREMATCH ($`) 和 POSTMATCH ($') 的 C++ 等价物?我会对指向此处的字符串、char * 或成对的 indices/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 似乎能够返回带有匹配项的 vector ,包括字符串的“结束”部分,因此理论上我可以提取这样的前/后变量,但这似乎需要很多工作。我喜欢 pcrecpp 界面的简单性。

建议?谢谢!

--埃里克

最佳答案

您可以使用 FullMatch 而不是 PartialMatch 并明确捕获自己的前后,例如

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

关于c++ - 有没有办法从 pcrecpp 获取 PREMATCH ( $`) and POSTMATCH ($' )?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2443031/

相关文章:

Perl 严格引用错误

c++ - 如何让 struct 一次只存储一个变量? C++

perl - 如何使用 HTML 表单将数组传递给 perl cgi 脚本?

c++ - 类重定义 C++ multiple include

打印完整二叉树的 JavaScript 函数

regex - 仅匹配文档中的一行,然后在 Ubuntu 上使用 bash 替换该行中的数字

c# - 不同年份格式的正则表达式 c#

perl - HTTP::Daemon::SSL 自动获取并提供中间证书

c++ - 在流中多次设置值

C++ 模板过度使用