如何从 Python 正则表达式中获取真/假?

pythonserver side programmingprogramming

当您使用模块 re 的 match 和 search 方法时,如果匹配,则其布尔值为 True,如果没有匹配,则获得 None,其布尔值为 False。

匹配对象始终为真,如果没有匹配,则返回 None

>>> bool(re.search("def", "abcdefgh"))
True
>>> bool(re.search("rest", "pqrstuv"))
False

相关文章


有用资源