* @brief Return all PCRE capture groups that matched in the subject string * @param subject PCRE subject string * @param result reference to vector of strings containing all capture groups */
| 228 | * @param result reference to vector of strings containing all capture groups |
| 229 | */ |
| 230 | bool |
| 231 | Pattern::capture(const String &subject, StringVector &result) |
| 232 | { |
| 233 | int matchCount; |
| 234 | int ovector[OVECOUNT]; |
| 235 | |
| 236 | PrefetchDebug("matching '%s' to '%s'", _pattern.c_str(), subject.c_str()); |
| 237 | |
| 238 | if (!_re) { |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | matchCount = pcre_exec(_re, nullptr, subject.c_str(), subject.length(), 0, PCRE_NOTEMPTY, ovector, OVECOUNT); |
| 243 | if (matchCount < 0) { |
| 244 | if (matchCount != PCRE_ERROR_NOMATCH) { |
| 245 | PrefetchError("matching error %d", matchCount); |
| 246 | } |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | for (int i = 0; i < matchCount; i++) { |
| 251 | int start = ovector[2 * i]; |
| 252 | int length = ovector[2 * i + 1] - ovector[2 * i]; |
| 253 | |
| 254 | String dst(subject, start, length); |
| 255 | |
| 256 | PrefetchDebug("capturing '%s' %d[%d,%d]", dst.c_str(), i, ovector[2 * i], ovector[2 * i + 1]); |
| 257 | result.push_back(dst); |
| 258 | } |
| 259 | |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * @brief Replaces all replacements found in the replacement string with what matched in the PCRE capturing groups. |