Skip to content

Commit 7b31d4c

Browse files
committed
Fix Pcre::searchAll behaviour wrt empty capturing groups
Previously, searchAll would stop search when it encountered an empty matching group in any position. This means that, for example, regular expression "(a)(b?)(c)" would match string "ac", but the resulting group list would be ["ac", "a"]. After this change, the resulting list for the aforementioned regular expression becomes ["ac", "a", "", "c"] like it should've been. Additionally, this also changes behaviour for multiple matches. For example, when "aaa00bbb" is matched by "[a-z]*", previously only "aaa" would be returned. Now the matching list is ["aaa", "", "", "bbb", ""]. The old behaviour was confusing and almost certainly a bug. The new behaviour is the same as in Python's re.findall. For reference, though, Go does it somewhat differently: empty matches at the end of non-empty matches are ignored, so in Go above example is ["aaa", "", "bbb"] instead.
1 parent d742dd2 commit 7b31d4c

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/regex/backend/pcre.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ std::list<RegexMatch> Pcre::searchAll(const std::string& s) const {
8787
std::string match = std::string(tmpString, start, len);
8888
offset = start + len;
8989
retList.push_front(RegexMatch(match, start));
90+
}
9091

91-
if (len == 0) {
92-
rc = 0;
93-
break;
94-
}
92+
offset = ovector[1]; // end
93+
if (offset == ovector[0]) { // start == end (size == 0)
94+
offset++;
9595
}
9696
} while (rc > 0);
9797

0 commit comments

Comments
 (0)