首页 > 解决方案 > Python:在字符串中查找确切的短语

问题描述

我对字符串操作不是很熟悉,我试图找到一种方法来确定给定一个文本字符串和一个短语,该短语是否按照给定的相同顺序出现在字符串中。

例子:
string='This question will probably be down voted because I am not able to post a minimal, complete, and verifiable example.'

phrase='I will not be down voted.'

由于 中的phrase没有以相同的顺序出现string,因此不匹配。我知道可以找到一个单词是否是字符串的一部分,例如:

if 'seek' in 'those who seek shall find':
    print('Success!')

但是如何检查整个短语是否以原始顺序出现在字符串中?

标签: pythonregexstring

解决方案


如果我做对了,您可以像在给定字符串中查找单词一样执行此操作:

if 'who seek shall' in 'those who seek shall find':
    print('Success!')

如果我对您的要求有误,请纠正我。


推荐阅读