开发者

Is there a way to get the PREMATCH ($`) and POSTMATCH ($') from pcrecpp?

开发者 https://www.devze.com 2022-12-23 09:15 出处:网络
Is there a way to obtain the C++ equivalent of Perl\'s PREMATCH ($`) and POSTMATCH ($\') from pcrecpp? I would be happy with a string, a char *, or pairs indices/startpos+length that point at this.

Is there a way to obtain the C++ equivalent of Perl's PREMATCH ($`) and POSTMATCH ($') from pcrecpp? I would be happy with a string, a char *, or pairs indices/startpos+length that point at this.

StringPiece seems like it might accomplish part of this, but I'm not certain how to get it.

in perl:

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

in C++ I would have something like:

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???
}开发者_Python百科

pcre plainjane c seems to have the ability to return the vector with the matches including the "end" portion of the string, so I could theoretically extract such a pre/post variable, but that seems like a lot of work. I like the simplicty of the pcrecpp interface.

Suggestions? Thanks!

--Eric


You could use FullMatch instead of PartialMatch and explicitly capture pre and post yourself, e.g.

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

精彩评论

暂无评论...
验证码 换一张
取 消