1

有没有办法从 pcrecpp 获得 Perl 的 PREMATCH ($`) 和 POSTMATCH ($') 的 C++ 等价物?我会很高兴有一个字符串、一个字符 * 或在这一点上的索引/起始位置 + 长度配对。

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 界面的简单性。

建议?谢谢!

——埃里克

4

1 回答 1

3

您可以使用FullMatch而不是PartialMatch明确地捕获 pre 和 post 自己,例如

string pre, match, post;
RE("(.*)(lo\\s)(.*)").FullMatch("Hello world", &pre, &match, &post);
于 2010-03-14T17:38:31.417 回答