Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
found = re.findall("g+", "fggfggggfggfg", re.DOTALL)
我想使用 findall 找到一个模式的最长匹配项。我找到了一些解决方案,但仅适用于re.matchor re.finditer。有人可以给我一个建议吗?
re.match
re.finditer
re.DOTALL在这种情况下什么都不做,所以为了简单起见,我只是把它拿出来:
re.DOTALL
>>> import re >>> max(re.findall("g+", "fggfggggfggfg"), key=len) 'gggg'
如果您需要按长度顺序排列它们:
>>> sorted(re.findall("g+", "fggfggggfggfg"), key=len, reverse=True) ['gggg', 'gg', 'gg', 'g']