Regex help

Bruce Edge bedge at troikanetworks.com
Fri Dec 7 19:21:05 EST 2001


In <mailman.1007766315.21802.python-list at python.org>, David A McInnis wrote:

> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <HTML><HEAD>
> <META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
> <META content="MSHTML 6.00.2600.0" name=GENERATOR></HEAD>
> <BODY>
> <DIV><SPAN class=513130223-07122001><FONT face=Arial size=2>Is there a more 
> efficient way to do this.  It seams like this is executing the search 
> twice.</FONT></SPAN></DIV>
> <DIV><SPAN class=513130223-07122001><FONT face=Arial 
> size=2></FONT></SPAN> </DIV>
> <DIV><SPAN class=513130223-07122001><FONT face=Arial size=2>    
> patt = re.compile("fax<br>\s*([0-9]{3,3}-[0-9]{3,3}-[0-9]{4,4})", 
> re.I)</FONT></SPAN></DIV>
> <DIV><SPAN class=513130223-07122001><FONT face=Arial size=2>    
> if patt.search(tststring):<BR>        faxnum 
> = patt.search(tststring).group(1)<BR>        
> print faxnum<BR></FONT></SPAN></DIV>
> <DIV><SPAN class=513130223-07122001><FONT face=Arial size=2>I cannot just do 
> </FONT></SPAN></DIV>
> <DIV><SPAN class=513130223-07122001><FONT face=Arial size=2>    
> faxnum = patt.search(tststring).group(1)</FONT></SPAN></DIV>
> <DIV><SPAN class=513130223-07122001><FONT face=Arial size=2><BR>because if there 
> is no match, it returns an error</FONT></SPAN></DIV>
> <DIV><SPAN class=513130223-07122001><FONT face=Arial 
> size=2></FONT></SPAN> </DIV>
> <DIV><SPAN class=513130223-07122001><FONT face=Arial 
> size=2>David</DIV></FONT></SPAN></BODY></HTML>
> 

Use match rather than search.



More information about the Python-list mailing list