python - 为什么 re.search 打印匹配发现即使字符串也不完全相同
问题描述
我的string_first
匹配pattern
和pattern_second
(见下面的代码)。但是,我希望它只与pattern_second
. 有人可以帮助我实现这一目标吗?
import re
string_first = "this-is-first-time"
pattern = "this-is-first"
pattern_second = "this-is-first.*"
if re.search(pattern, string_first):
print("string match for pattern first")
else:
print("string not match")
if re.search(pattern_second, string_first):
print("string match for pattern second")
else:
print("string not match")
解决方案
在您的模式中指定您的行尾如下:
pattern = "this-is-first$"
'$' 表示行尾。
推荐阅读
- c - 为什么输出末尾有随机字符?
- laravel - laravel 模型控制器检索数据
- java - 尝试将布局设置为 BoxLayout
- c++ - 如何解决“if”块中的“未声明的标识符错误”?
- android - 如何在 ViewModel 中获取字符串值,例如 R.string.value
- reactjs - 无法从“C:\repos\ondemandProject\ondemnd-web”中找到模块“@babel/preset-es2015”
- swift - 如何设置当前线程的语言环境?
- python - 在没有应用的情况下计算熊猫中多列的条纹
- haskell - Haskell 中的 Where 子句使用 Data.Map
- android - exo播放器没找到?