【发布时间】:2014-04-04 17:14:20
【问题描述】:
sentence = 'Alice was not a bit hurt, and she jumped up on to her feet in a moment.'
words = ['Alice','jumped','played']
为了匹配words 中的sentence,我使用了last post 答案中的代码
[w for w in words if re.search(r'\b{}\b'.format(re.escape(w)), sentence)]
这会得到我:
['Alice', 'jumped']
现在,如果words 列表以另一个序列(words = ['jumped','Alice','played'])给出,我想以它们在sentence 中出现的顺序显示匹配结果,即,仍然想要:
['Alice', 'jumped']
而不是
['jumped','Alice']
我应该如何修改代码?
【问题讨论】:
-
如果句子是'Alice jumped over Alice',你想得到什么?
标签: python regex python-2.7