您能帮我得到每次出现两个字符之间的子字符串吗?

例如,在所有出现的情况下,获取给定示例序列中“Q”和“E”之间的所有子字符串:

ex: QUWESEADFQDFSAEDFS

并找到具有最小长度的子字符串。

最佳答案

import re
DATA = "QUWESEADFQDFSAEDFS"

# Get all the substrings between Q and E:
substrings = re.findall(r'Q([^E]+)E', DATA)
print "Substrings:", substrings

# Sort by length, then the first one is the shortest:
substrings.sort(key=lambda s: len(s))
print "Shortest substring:", substrings[0]

09-26 17:22