check if regeular expression has results

Steve Holden steve at holdenweb.com
Thu Aug 9 09:17:08 EDT 2007


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?
> 

A failed match returns None. A successful match returns a match object. 
So the easiest way to check for a successful match is

pat = re.compile(...)
     ....
m = pat.match(some_string)
if m:
     ... you got a match ...
else:
     ... you didn't ...

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list