Python regular expressions return true/false

前端 未结 6 1236
情歌与酒
情歌与酒 2020-12-23 02:44

Using Python regular expressions how can you get a True/False returned? All Python returns is:

<_sre.SRE_Match object at ...>         


        
6条回答
  •  猫巷女王i
    2020-12-23 03:05

    If you really need True or False, just use bool

    >>> bool(re.search("hi", "abcdefghijkl"))
    True
    >>> bool(re.search("hi", "abcdefgijkl"))
    False
    

    As other answers have pointed out, if you are just using it as a condition for an if or while, you can use it directly without wrapping in bool()

提交回复
热议问题