Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python字符串中查找重复模式_Python_Regex - Fatal编程技术网

在Python字符串中查找重复模式

在Python字符串中查找重复模式,python,regex,Python,Regex,假设我有这根绳子 s='123123' 我可以注意到“123”子字符串正在重复 here='1234' 子字符串将是“1234”,没有重复 s='11111' 子字符串将是“1” 我怎样才能用Python得到这个呢?有什么提示吗 strings = ['123123123', '1234', '11111'] import re pattern, result = re.compile(r'(.+?)\1+'), [] for item in strings: result.extend

假设我有这根绳子

s='123123'

我可以注意到“123”子字符串正在重复

here='1234'

子字符串将是“1234”,没有重复

s='11111'

子字符串将是“1”

我怎样才能用Python得到这个呢?有什么提示吗

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']


您可以看到正则表达式的解释,但他希望检测中间字符串的1234。@AaronHall噢,谢谢人:)修复了:)您不能在正则表达式中修复它吗?@AaronHall如果字符串是
11111 2111
,它可能会断开?