std::string replace_with_pattern(const std::string& item, const RE2& pattern, const std::string& target) { RE2::Arg argv[10]; const RE2::Arg* args[10] = {&argv[0], &argv[1], &argv[2], &argv[3], &argv[4], &argv[5], &argv[6], &argv[7], &argv[8], &argv[9]}; std::string arg[10]; for (size_t i = 0; i < 10; i++) { argv[i] = &arg[i]; } if (!RE2::FullMatchN(item, pattern, args, pattern.NumberOfCapturingGroups())) return item; return replace_matches(target, arg, '@', pattern.NumberOfCapturingGroups()); }
int msc_regexec_ex(RE2 &re, const char *s, unsigned int slen, int startoffset, int *ovector, int ovecsize) { size_t startpos = startoffset; const size_t endpos = slen; // Total # of submatches in the regex pattern int num_submatch = 1 + re.NumberOfCapturingGroups(); // Index of the last non empty submatch int last_nonempty_submatch = num_submatch - 1; re2::StringPiece submatches[num_submatch]; // If the string does not match the pattern if (!re.Match(s, startpos, endpos, RE2::UNANCHORED, submatches, num_submatch)) { return -1; } // Find the last non empty submatch while (!submatches[last_nonempty_submatch].data()) { last_nonempty_submatch--; } int count = min(last_nonempty_submatch + 1, ovecsize / 3); // Extract submatch information as much as possible for (int i = 0; i < count; i++) { // An empty submatch if (!submatches[i].data()) { ovector[2 * i] = -1; ovector[2 * i + 1] = -1; } else { ovector[2 * i] = submatches[i].data() - s; ovector[2 * i + 1] = ovector[2 * i] + submatches[i].length(); } } // The output vector has enough space to store the information of // all non empty submatches + empty submatches among non empty submatches if (last_nonempty_submatch + 1 <= ovecsize / 3) { return last_nonempty_submatch + 1; } // Truncate empty submatches at the tail of 'ovector' if (!submatches[ovecsize / 3 - 1].data()) { for (int i = ovecsize / 3 - 2; i >= 0; i--) { if (submatches[i].data()) { return i + 1; } } } return 0; }
JNIEXPORT jint JNICALL Java_com_logentries_re2_RE2_numberOfCapturingGroupsImpl (JNIEnv *env, jclass cls, jlong re2_pointer) { RE2 *regex = reinterpret_cast<RE2*>(re2_pointer); return static_cast<jint>(regex->NumberOfCapturingGroups()); }