在Python字符串中查找重复模式
假设我有这根绳子 s='123123' 我可以注意到“123”子字符串正在重复 here='1234' 子字符串将是“1234”,没有重复 s='11111' 子字符串将是“1” 我怎样才能用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
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
,它可能会断开?