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