Finding the recurring pattern

梦想与她 提交于 2019-12-01 05:34:06
vks
(.+?)\1+

Try this. Grab the capture. See demo.

import re
p = re.compile(ur'(.+?)\1+')
test_str = u"1234123412341234"

re.findall(p, test_str)

Add anchors and flag Multiline if you want the regex to fail on 12341234123123, which should return None.

^(.+?)\1+$

See demo.

One way to find a recurring pattern and number of times repeated is to use this pattern:

(.+?)(?=\1+$|$)  

w/ g option.
It will return the repeated pattern and number of matches (times repeated)
Non-repeated patterns (fails) will return only "1" match
Repeated patterns will return 2 or more matches (number of times repeated).
Demo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!