what should the results be, of the regular expression "(aa|aaa)(abbb|bbb)"?
$1 = ?
$2 = ?
what should the results be, of the regular expression "(aa|aaa)(abbb|bbb)"?
$1 = ?
$2 = ?
- the whole string, grouped as "(aa)(abbb)",
- the whole string, grouped as "(aaa)(bbb)";
- the substring "aabbb", grouped as "(aa)(bbb)".
This was a small regex designed to create multiple answers to see how you resolved the issue, obviously we can engineer regexes that return far more results. So something's got to give. I don't agree with you that regex's innately imply all matches are valid.
Python's regex library, for example, can return multiple matches. It has three functions:
- `re.match`, which checks whether the whole string matches.
- `re.search`, which checks for the first location in the string that matches.
- `re.findall`, which finds "all" non-overlapping matches.
I was simply suggesting that the "non-overlapping" constraint in findall is a "bug", in some sense, because it exposes implementation details of the regex engine.
But, again, given that it is apparently a useful bug, maybe I am wrong. But that leaves open the question what the right spec for regex matching is, anyway.