Returns true iff regex matches any substring of str. regex must be a valid simple regular expression, or the result is undefined. The algorithm is recursive, but the recursion depth doesn't exceed the regex length, so we won't need to worry about running out of stack space normally. In rare cases the time complexity can be exponential with respect to the regex length + the string length, but us
| 8157 | // exponential with respect to the regex length + the string length, |
| 8158 | // but usually it's must faster (often close to linear). |
| 8159 | bool MatchRegexAnywhere(const char* regex, const char* str) { |
| 8160 | if (regex == NULL || str == NULL) |
| 8161 | return false; |
| 8162 | |
| 8163 | if (*regex == '^') |
| 8164 | return MatchRegexAtHead(regex + 1, str); |
| 8165 | |
| 8166 | // A successful match can be anywhere in str. |
| 8167 | do { |
| 8168 | if (MatchRegexAtHead(regex, str)) |
| 8169 | return true; |
| 8170 | } while (*str++ != '\0'); |
| 8171 | return false; |
| 8172 | } |
| 8173 | |
| 8174 | // Implements the RE class. |
| 8175 |
no test coverage detected