Skip to main content
 首页 » 编程设计

python之为什么 re.match ("c", "cat") 不返回 true

2025年05月04日11dudu

为什么

not re.match("c", "cat") 

返回假,但是

re.match("c", "cat") 

不返回 True 而是返回对象在内存中的位置。我找不到使此语句返回 true 的方法,但我知道它是 true,因为:

if re.match("c", "cat"): 
    print "Yes!" 

返回“是!”。

正如我所说,这没有任何实际意义,至少目前没有,但它确实让我感到困惑。

请您参考如下方法:

使用bool()转换为 bool 值(true / false):

bool(re.match("c", "cat")) == true 

当你在 if 语句中使用 re.match("c", "cat") 时,它会自动转换为 bool 值,true ,这就是为什么它会返回 Yes!

使用 not 会自动将其转换为 bool 值,然后将其反转,因此:

not re.match("c", "cat") == false