I am using Python 3.6.
My goal is to match a regex which may match multiple strings, overlapping and/or starting from the same position, for example:
re.findall('B.*A','BADACBA')
which gives:
['BADACBA']
But I want:
['BADACBA','BADA','BA','BA']
(the second 'BA'
is because there are two instances of 'BA'
in the string)
On the suggestion of How to find overlapping matches with a regexp?, I've tried encasing it in a lookahead:
re.findall('(?=(B.*A))','BADACBA')
which gives:
['BADACBA', 'BA']
which is better, but still not what I want.
I also tried the regex
module:
regex.findall('B.*A','BADACBA',overlapped=True)
but it still returns:
['BADACBA', 'BA']
I haven't been able to find something that will find all matches. Since I have many such regexes, a hard-coded solution won't help much. Is there a module/function that does this?
Thanks!