2010-03-14 35 views
1

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

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Ç似乎已经返回与匹配包括 “端” 部分中的矢量能力的字符串,所以我理论上可以提取这样的前/后变量,但这似乎是很多工作。我喜欢pcrecpp界面的简单性。

对此提出建议?谢谢!

--Eric

回答

3

你可以使用的FullMatch代替PartialMatch,明确捕获前和发布自己,例如

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