3

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?

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
OHHH
  • 1,011
  • 3
  • 16
  • 34

1 Answers1

3
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

thefourtheye
  • 233,700
  • 52
  • 457
  • 497