check if regeular expression has results

Patrick Doyle wpdster at gmail.com
Thu Aug 9 09:12:51 EDT 2007


On 8/9/07, shahargs at gmail.com <shahargs at gmail.com> 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.

--wpd



More information about the Python-list mailing list