5

I'm attempting to match many (500+) regular expressions quickly using Google's RE2 Library, as I'd like to get similar results to this whitepaper. I'd like to use RE2-m on page 13.

From what I've seen online, the Set interface is the way to go, though I'm unsure where to get started -- I haven't been able to find Google RE2 tutorials using the set interface online. Could someone please point me in the right direction?

EnnFour
  • 323
  • 2
  • 8

1 Answers1

3

Just implemented this today for something I'm working on, here is a snippet for the use of future readers.

The right class to handle this using RE2 is RE2::Set, you can find the code here.

Here is an example:

std::vector<std::string> kRegexExpressions = {
  R"My name is [\w]+",
  R"His number is [\d]+",
};

RE2::Set regex_set(RE2::DefaultOptions, RE2::UNANCHORED);

for (const auto &exp : kRegexExpressions) {
  int index = regex_set.Add(exp, &err);
  if (index < 0) {
    <report-error>
    return;
  }
}

if (!regex_set.Compile()) {
  <report-error>
  return;
}

std::vector<int> matching_rules;
if (!regex_set_.Match(line, &matching_rules)) {
  <no-match>
  return;
}

for (auto rule_index : matching_rules) {
  std::cout << "MATCH: Rule #" << rule_index << ": " << kRegexExpressions << std::endl;
}
Daniel Trugman
  • 8,186
  • 20
  • 41