I am trying to find all the occurrences of e.g. p='gg'
inside s='ggggg'
. Based on my count there should be 4 since any position apart from the last one is a substring. For example s[1:2]
is 'gg'
. However, trying both:
>re.findall('gg','ggggg')
['gg','gg']
>list(re.finditer('gg','ggggg'))
[<_sre.SRE_Match object; span=(0, 2), match='ab'>,
<_sre.SRE_Match object; span=(6, 8), match='gg'>,
<_sre.SRE_Match object; span=(8, 10), match='gg'>]
Seems to be skipping over potential matches once it finds some match. Also, as a result, the search for e.g. 'star' or 'start' is equivalent to just looking for start, since I would never find the second because the first is its prefix...
Is this a bug? How can I perform a full substring search ?
example 2:
>re.findall('star|start','starting')
['star']
>list(re.finditer('star|start','starting'))
[<_sre.SRE_Match object; span=(0, 4), match='star'>]
(I am using Python 3, re version 2.2.1)