你能帮我在每次出现时得到两个字符之间的子串吗
例如,在所有出现的给定示例序列中获取“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]
关于python - 在 python 中查找子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/788699/