我有一个随机字符串,例如:
a = "aabcaaabbccccdabbbbbc"
我想找出最长的重复子串及其长度。在这种情况下,答案应该是“bbbbb”和5。什么是正则表达式方式?
import re
a = "aabcaaabbccccdabbbbbc"
pattern = re.compile(r'(\w)(\1)+')
max_pattern_len, max_pattern = 0, ''
for match in pattern.finditer(a):
m = match.group()
if len(m) > max_pattern_len:
max_pattern_len = len(m)
max_pattern = m
print(max_pattern_len, max_pattern)
输出:
(5, 'bbbbb')