check if regeular expression has results

  • Thread starter Bruno Desthuilliers
  • Start date
S

shahargs

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?

Thanks a lot,
Shahar.
 
M

Marc 'BlackJack' Rintsch

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?

Simply use an ``if`` on the result of the search or match. If the regular
expression doesn't match `None` is returned, which is `False` in a boolean
context, otherwise a match object is returned, which is `True` in a
boolean context.

Ciao,
Marc 'BlackJack' Rintsch
 
P

Patrick Doyle

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
 
N

Neil Cerutti

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?

Newgroups are a poor substitute for the docs. For one thing,
newsgroups sometimes contain cranky people who say, "RTFM!" The
docs will never do that.
 
S

Steve Holden

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top