How best to write this if-else?

Roy Smith roy at panix.com
Sat Apr 21 17:01:35 EDT 2001


"Ingo Wilken" <Ingo.Wilken at Informatik.Uni-Oldenburg.DE> wrote:
> for e in [e1, e2, e3]:
>     m = e.match(line)
>     if m:
>         text = m.group(1)
>         break
> else:
>     no match found

OK, that's pretty neat, but I realize now that my example was 
unintentionally misleading.  The problem is not quite as regular as I made 
it out to be.

What if I want to execute different code depending on which expression I 
matched?  Something along the lines of (pseudocode):

if (m = e1.match(line)):
   text1 = m.group(1)
   do_complicated_processing (text1)
elif (m = e2.match(line)):
   text1 = m.group(1)
   text2 = m.group(2)
   print text1, text2
elif (m = e3.match(line)):
   return



More information about the Python-list mailing list