Alex Rivera | Logout

Python regular expressions return true/false

Asked 2011-07-04T23:57:41.687
188

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

<_sre.SRE_Match object at ...>
Edit
Report

1 Answer

9

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.

answered 2011-07-05T00:04:18.777

Your Answer