Regex help

Jason Orendorff jason at jorendorff.com
Fri Dec 7 19:20:27 EST 2001


> 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

Here's what I always do:

  match = patt.search(tststring)
  if match:
      faxnum = match.group(1)
      print faxnum

Also, it is wise to put an r on every regex string:
  patt = re.compile(r"fax......", re.I)
                    ^

-- 
Jason Orendorff
http://www.jorendorff.com/





More information about the Python-list mailing list