[英]Python - finding the longest sequence with findall
found = re.findall("g+", "fggfggggfggfg", re.DOTALL)
I'd like to find a longest matches for a pattern using findall. 我想使用findall为模式找到最长的匹配项。 I've found some solutions but only for re.match
or re.finditer
. 我找到了一些解决方案,但仅限于re.match
或re.finditer
。 Could anybody give me an advice please? 请问有人给我一个建议吗?
re.DOTALL
does nothing in this case so I've just taken it out for simplicity's sake: re.DOTALL
在这种情况下什么都不做,所以为了简单起见我刚把它拿出来:
>>> import re
>>> max(re.findall("g+", "fggfggggfggfg"), key=len)
'gggg'
If you need all of them in order of length: 如果您需要按长度顺序排列所有这些:
>>> sorted(re.findall("g+", "fggfggggfggfg"), key=len, reverse=True)
['gggg', 'gg', 'gg', 'g']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.