【发布时间】:2017-07-18 13:42:20
【问题描述】:
我需要找到可变长度字符序列的开始和结束位置,由字符串中的相同 1 个字母组成。 我看到这个话题Finding multiple occurrences of a string within a string in Python,但我认为它有点离题了。
以下内容没有给我任何信息,而我希望找到 5 个元素。
import re
s = 'aaaaabaaaabaaabaaba'
pattern = '(a)\1+'
for el in re.finditer(pattern, s):
print 'str found', el.start(), el.end()
提前致谢。
【问题讨论】:
-
It works well, just use a raw string literal。你只能期待 4 个结果。否则,将
+替换为*。 -
Wiktor Stribiżew,谢谢你,尤其是关于 * 的提示。