Regex to remove repeated character pattern in a string

前端 未结 4 1702
梦毁少年i
梦毁少年i 2020-12-14 22:30

I have a string that may have a repeated character pattern, e.g.

\'xyzzyxxyzzyxxyzzyx\'

I need to write a regex that would replace such str

4条回答
  •  青春惊慌失措
    2020-12-14 23:23

    Since you want the smallest repeating pattern, something like the following should work for you:

    re.sub(r'^(.+?)\1+$', r'\1', input_string)
    

    The ^ and $ anchors make sure you don't get matches in the middle of the string, and by using .+? instead of just .+ you will get the shortest pattern (compare results using a string like 'aaaaaaaaaa').

提交回复
热议问题