python - 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!')
但是如何检查整个短语是否以原始顺序出现在字符串中?
解决方案
如果我做对了,您可以像在给定字符串中查找单词一样执行此操作:
if 'who seek shall' in 'those who seek shall find':
print('Success!')
如果我对您的要求有误,请纠正我。
推荐阅读
- javascript - 无法使用 React Native 中的对象访问数组中的值
- tcl - 正则表达式匹配输出,而它不应该
- php - 未定义的索引错误 & 没有来自 POST/GET 的输出
- ios - 无法将 Swift 代码导入到 Objective-C 项目
- elasticsearch - ELK 堆栈中的 Logstash 和 filebeat
- reactjs - 有没有办法关闭 LinkedIn 验证器代码?
- python - 从 pandas 数据框列中的 pandas.core.series.Series 中提取日期
- excel - 如何在django中用空格指定列名
- c# - 接口不能隐式转换类型
- php - 使用 AJAX 和 jQuery 验证 From 而不将数据发送到另一个文件