How to do this in Python...

Chad Netzer cnetzer at mail.arc.nasa.gov
Tue Jan 28 04:17:40 CET 2003


On Monday 27 January 2003 18:18, Martin Maney wrote:
> Of course tastes differ when you're twisting a
> tool into doing something it doesn't wish to do, which was perhaps
> the ultimate point of my small treatise on contorted Python.

Stefan Schwarzer had the best (IMO) solution to your query, and it is 
an approach I use often with Python.  Far from contorted, I'd say.  
Anyway, it seemed to fit the bill nicely.

Here is is again (excerpted):


potential_patterns = (pattern1, pattern2, pattern3)
for pattern in potential_patterns:
     match = re.match(pattern, string)
     if match:
         # found it, do something, then ...
         break
else:
     # didn't find it (if you need this branch at all)


-- 
Bay Area Python Interest Group - http://www.baypiggies.net/

Chad Netzer
(any opinion expressed is my own and not NASA's or my employer's)





More information about the Python-list mailing list