Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the offset of a matching string using RE2?

Tags:

c++

regex

re2

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?

like image 559
vy32 Avatar asked Aug 11 '12 22:08

vy32


People also ask

How do I create an offset and match formula?

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

How to use offset and match in MySQL?

= 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.

Should I use offset match match or index as my lookup formula?

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.

What are the requirements for using offset match match?

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


1 Answers

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";
    }
  }
}
like image 165
Robᵩ Avatar answered Nov 15 '22 01:11

Robᵩ