RE2 is a modern regular expression engine available from Google. I want to use RE2 in a program that is currently using gnuregex. The problem I have relates to finding out what matched. What RE2 returns is the string that matched. I need to know the offset of what matched. My current plan is to take what RE2 returns and then use a find
on the C++ string. But this seems wasteful. I've gone through the RE2 manual and can't figure out how to do it. Any ideas?
Step 1: Start writing your OFFSET formula and select your starting reference point, which will be the upper left hand corner of your table. In this case it’s the cell containing the word “Country”. Step 2: Start your MATCH formula and select your vertical lookup value, in this case, the country China
= OFFSET ( starting point , MATCH ( vertical lookup value , left hand lookup column excluding starting point , 0 ) , MATCH ( horizontal lookup value , top header row excluding starting point , 0 ) ) Step 1: Start writing your OFFSET formula and select your starting reference point, which will be the upper left hand corner of your table.
Additionally, some people are more familiar with the OFFSET function than they are with INDEX; in that situation, it may make sense to go with OFFSET MATCH MATCH as your lookup formula. There’s only one simple requirement for using OFFSET MATCH MATCH: the need to perform a matrix style lookup.
There’s only one simple requirement for using OFFSET MATCH MATCH: the need to perform a matrix style lookup. To put it in simple terms: The data table you use must have lookup values on both the left hand side as well as the top header row
Store the result in a re2::StringPiece
instead of a std::string
. The value of .data()
will point into the original string.
Consider this program.
In each of the tests, result.data()
is a pointer into the original const char*
or std::string
.
#include <re2/re2.h>
#include <iostream>
int main(void) {
{ // Try it once with character pointers
const char *text[] = { "Once", "in", "Persia", "reigned", "a", "king" };
for(int i = 0; i < 6; i++) {
re2::StringPiece result;
if(RE2::PartialMatch(text[i], "([aeiou])", &result))
std::cout << "First lower-case vowel at " << result.data() - text[i] << "\n";
else
std::cout << "No lower-case vowel\n";
}
}
{ // Try it once with std::string
std::string text[] = { "While", "I", "pondered,", "weak", "and", "weary" };
for(int i = 0; i < 6; i++) {
re2::StringPiece result;
if(RE2::PartialMatch(text[i], "([aeiou])", &result))
std::cout << "First lower-case vowel at " << result.data() - text[i].data() << "\n";
else
std::cout << "No lower-case vowel\n";
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With