更新时间:2023-02-20 22:49:38
使用以下内容:
> re.sub(r'(.+?)\1+', r'\1', 'xyzzyxxyzzyxxyzzyx')
'xyzzyx'
> re.sub(r'(.+?)\1+', r'\1', 'abcbaccbaabcbaccbaabcbaccba')
'abcbaccba'
> re.sub(r'(.+?)\1+', r'\1', 'iiiiiiiiiiiiiiiiii')
'i'
它基本上匹配重复自身的模式(.+?)\1+
,并删除除重复模式之外的所有内容,该模式在第一组\1
中被捕获>.还要注意,在这里使用不情愿的限定符,即 +?
会使正则表达式回溯很多.
It basically matches a pattern that repeats itself (.+?)\1+
, and removes everything but the repeating pattern, which is captured in the first group \1
. Also note that using a reluctant qualifier here, i.e., +?
will make the regex backtrack quite a lot.
演示.