4 ms·
Given a string "aaabbb" what should the results be, of the regular expression "(aa|aaa)(abbb|bbb)"? $1 = ? $2 = ?
by GhotiFish 11y ago
Given a string "aaabbb"
what should the results be, of the regular expression "(aa|aaa)(abbb|bbb)"?
$1 = ?
$2 = ?
- rntz 11y agoI believe it matches in three ways: - the whole string, grouped as "(aa)(abbb)", - the whole string, grouped as "(aaa)(bbb)"; - the substring "aabbb", grouped as "(aa)(bbb)".
- GhotiFish 11y agoSorry, I made an assumption there that you were talking about the practical applications of regex, not the theoretical applications, and I was asking you to explain how you would practically return multiple matches in... any environment. 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.
- rntz 11y agoThe original article already relies on finding multiple matches, in order to ignore the matches that don't contain the group that we're interested in. 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.