tags:

views:

38

answers:

1

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???
}

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

+3  A: 

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);
Logan Capaldo