check if regeular expression has results

Patrick Doyle wpdster at
Thu Aug 9 15:12:51 CEST 2007

On 8/9/07, shahargs at <shahargs at> wrote:
> Hi,
> I'm looking for the best way to check if regular expression return
> true (it's mean - there is a match). for example, i want "if" that
> check if this regular expression: .*born.*to.* has a match.
> What's the way to do that simply?
How about

import re
re.match(".*born.*to", "This is a test")
re.match(".*born.*to.*", "This test was born so that it worked too.")

(Try these at the python prompt)

The first call to 're.match()' returns 'None' which will fail an if
test.  The second one returns a match object, which evaluates to TRUE
in an if test.


More information about the Python-list mailing list