python - 在 python 中查找子字符串

标签 python regex algorithm substring

你能帮我在每次出现时得到两个字符之间的子串吗

例如,在所有出现的给定示例序列中获取“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/

相关文章:

r - 写算法的错误

windows - Windows 10 使用什么哈希算法来存储密码?

python - Pyramid 检测照片上传

python - Doc2vec 内存错误

python捕获异常

python - numpy.arctanh(x) for x >= 1 返回 NaN 但我想要复数

Java正则表达式匹配字典范围的字符串

python 正则表达式查找和散列用户名

php - 匹配以 pics.domain.com 开头的所有图像

javascript - 使用多个定界符和 Concat 拆分字符串