Given the string bcacaca. The regex pattern b?(.a)*
should match on the whole string. However, a call to re.findall('b?(.a)*', 'bcacaca')
returns ['ca', '']
as a result. It seems to be only returning the result of matching on the individual groups. What's going on here?
My understanding of findall
is that it should return all nonoverlapping instances of the regex pattern. In this case it should return ['bcacaca', '']
.
findall
works: "If one or more groups are present in the pattern, return a list of groups". If you want to match the whole string, change the group to non-capturingb?(?:.a)*
?:
is included at the start of every group that you want to mark as non-capturing?