Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you translate this regular-expression idiom from Perl into Python?

Tags:

python

regex

perl

I switched from Perl to Python about a year ago and haven't looked back. There is only one idiom that I've ever found I can do more easily in Perl than in Python:

if ($var =~ /foo(.+)/) {   # do something with $1 } elsif ($var =~ /bar(.+)/) {   # do something with $1 } elsif ($var =~ /baz(.+)/) {   # do something with $1 } 

The corresponding Python code is not so elegant since the if statements keep getting nested:

m = re.search(r'foo(.+)', var) if m:   # do something with m.group(1) else:   m = re.search(r'bar(.+)', var)   if m:     # do something with m.group(1)   else:     m = re.search(r'baz(.+)', var)     if m:       # do something with m.group(2) 

Does anyone have an elegant way to reproduce this pattern in Python? I've seen anonymous function dispatch tables used, but those seem kind of unwieldy to me for a small number of regular expressions...

like image 576
Dan Lenski Avatar asked Sep 23 '08 16:09

Dan Lenski


1 Answers

Using named groups and a dispatch table:

r = re.compile(r'(?P<cmd>foo|bar|baz)(?P<data>.+)')  def do_foo(data):     ...  def do_bar(data):     ...  def do_baz(data):     ...  dispatch = {     'foo': do_foo,     'bar': do_bar,     'baz': do_baz, }   m = r.match(var) if m:     dispatch[m.group('cmd')](m.group('data')) 

With a little bit of introspection you can auto-generate the regexp and the dispatch table.

like image 104
Thomas Wouters Avatar answered Oct 09 '22 18:10

Thomas Wouters