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'] ?
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'] ?
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.
Source: https://ru.stackoverflow.com/questions/881481/
All Articles