Python regular expressions return true/false

前端 未结 6 1216
情歌与酒
情歌与酒 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条回答
  •  再見小時候
    2020-12-23 03:16

    One way to do this is just to test against the return value. Because you're getting <_sre.SRE_Match object at ...> it means that this will evaluate to true. When the regular expression isn't matched you'll the return value None, which evaluates to false.

    import re
    
    if re.search("c", "abcdef"):
        print "hi"
    

    Produces hi as output.

提交回复
热议问题