That horrible regexp idiom

Nick Coghlan ncoghlan at iinet.net.au
Thu Feb 10 08:46:18 EST 2005


Stephen Thorne wrote:
> Hi,
> 
> import re
> foo_pattern = re.compile('foo')
> 
> '>>> m = foo_pattern.search(subject)
> '>>> if m:
> '>>>    pass
> '>>> else:
> '>>>    pass

Heh. Did you see Ray Gibbons's 'Testing Conditions' post before you sent this?

I knew if/elif was a much better argument in favour of embedded assignment than 
while loops are.

Anyway, here's the above using my idle thought from that the thread.:

   if m using foo_pattern.search(subject) as m:
     pass
   else:
     pass

And the 'alternative patterns' case:

   if m using foo_pattern.search(subject) as m:
     pass
   elif m using bar_pattern.search(subject) as m:
     pass
   else:
     pass

(Y'know, I'm pretty sure the impetus was regexp matching the *last* time 
embedded assignment came up. . .)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list