How to find the offset of the corresponding string using RE2?

RE2 is a modern regex engine available from Google. I want to use RE2 in a program that currently uses gnuregex. The problem I am having is figuring out what was appropriate. That RE2 returns matches the string. I need to know the offset of what matched. My current plan is to take the returned RE2 and then use it findin a C ++ string. But that seems wasteful. I went through the RE2 manual and cannot figure out how to do this. Any ideas?

+5
source share
1 answer

Save the result in re2::StringPieceinstead std::string. The value .data()will point to the source string.

Consider this program. In each of the tests result.data()is a pointer to 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";
    }
  }
}
+10
source

All Articles