我的任务
我正在尝试使用正则表达式查找出现在字符串中的单词位置。
代码
import re
# A random string
mystr = "there not what is jake can do for you ask what you play do for spare jake".upper()
match = re.search(r"[^a-zA-Z](jake)[^a-zA-Z]", mystr)
print match.start(1)
输出
18
期望输出
我期望我的输出中包含字符串jake
的位置:
5, 17
编辑:为了澄清,我正在尝试确定单词的位置。我相信我已经找到了索引,但不确定如何使其按照我的期望工作。
r'[\s]*(jake)[\s]*'
已经是更好的正则表达式了,但我认为仅使用正则表达式是不可能实现的。 - Ludisposed