How to select all necessary sequences from a string, including overlapping ones?
If I do this:

re.findall('\d{3}', '123456') 

it gives: ['123', '456']

How to make so that issued: ['123', '234', '345', '456'] ?

    1 answer 1

    You do not need regular expressions for your problem:

     In[65]: strng = '123456' In[66]: [strng[i:i+3] for i in range(len(strng)-2)] 
     Out[66]: ['123', '234', '345', '456'] 

    But when you use a regular expression for some reason, you can do so.

     In[67]: pattern = re.compile(r'\d{3}') In[68]: [pattern.match(strng[i:]).group() ...: for i in range(len(strng)-1) if pattern.match(strng[i:])] 
     Out[68]: ['123', '234', '345', '456'] 

    The fact is that we start from the whole line and gradually remove the symbols from it to the left.