Finding the recurring pattern

后端 未结 2 594
青春惊慌失措
青春惊慌失措 2021-01-13 11:21

Let\'s say I have a number with a recurring pattern, i.e. there exists a string of digits that repeat themselves in order to make the number in question. For example, such a

2条回答
  •  轮回少年
    2021-01-13 11:35

    (.+?)\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.

提交回复
热议问题