I have a regex that can match a string in multiple overlapping possible ways. However, it seems to only capture one possible match in the string, how can I get all possible
No problem:
>>> regex = "([^-]*-)(?=([^-]*))"
>>> for result in re.finditer(regex, "foo-foobar-foobaz"):
>>> print("".join(result.groups()))
foo-foobar
foobar-foobaz
By putting the second capturing parenthesis in a lookahead assertion, you can capture its contents without consuming it in the overall match.
I've also used [^-]*
instead of .*
because the dot also matches the separator -
which you probably don't want.