Regex help
Peter Hansen
peter at engcorp.com
Fri Dec 7 20:23:50 EST 2001
> David A McInnis wrote:
>
> Is there a more efficient way to do this. It seams like this is executing the search twice.
>
> patt = re.compile("fax<br>\s*([0-9]{3,3}-[0-9]{3,3}-[0-9]{4,4})", re.I)
> if patt.search(tststring):
> faxnum = patt.search(tststring).group(1)
> print faxnum
> I cannot just do
> faxnum = patt.search(tststring).group(1)
>
> because if there is no match, it returns an error
You could just encapsulate it in a try/except to catch the AttributeError
you get when it fails.
try:
print patt.search(tststring).group(1)
except AttributeError:
pass
--
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com
More information about the Python-list
mailing list