Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding a repetitive pattern in Python strings [duplicate]

Tags:

python

regex

Let's suppose I have this string

s = '123123123'

I can notice the '123' sub-string is being repeated.

here = '1234'

The sub-string would be '1234' with no repetitions.

s = '11111'

The sub-string would be '1'

How can I get this with Python? Any hints?

like image 709
OHHH Avatar asked Dec 29 '25 16:12

OHHH


1 Answers

strings = ['123123123', '1234', '11111']
import re
pattern, result = re.compile(r'(.+?)\1+'), []
for item in strings:
    result.extend(pattern.findall(item) or [item])
print result
# ['123', '1234', '1']

Regular expression visualization

Debuggex Demo

You can see the explanation for the RegEx here

like image 71
thefourtheye Avatar answered Dec 31 '25 08:12

thefourtheye