2010-03-14 97 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); 
相關問題