in Education by
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... JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Using named groups and a dispatch table: r = re.compile(r'(?Pfoo|bar|baz)(?P.+)') 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.

Related questions

0 votes
    I switched from Perl to Python about a year ago and haven't looked back. There is only one idiom ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    Each time a python file is imported that contains a large quantity of static regular expressions, cpu cycles ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    Each time a python file is imported that contains a large quantity of static regular expressions, cpu cycles ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    Each time a python file is imported that contains a large quantity of static regular expressions, cpu cycles ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    How will you convert a object to a regular expression in python?...
asked Nov 24, 2020 in Technology by JackTerrance
0 votes
    I'm trying to formulate how to write one regex to match this: I would like to write a regex to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    I'm trying to formulate how to write one regex to match this: I would like to write a regex to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I'm trying to formulate how to write one regex to match this: I would like to write a regex to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Can you write down a general regular expression for extracting the IP address from logs?...
asked Oct 31, 2020 in Technology by JackTerrance
0 votes
    On many sites/blogs I noticed that people, who want to explain linq mechanism, write Linq Queries and ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 15, 2022 in Education by JackTerrance
0 votes
    I need to replace each character of a regular expression, once evaluated, with each character plus the @ ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I need to replace each character of a regular expression, once evaluated, with each character plus the @ ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    import re text = 'The quick. black n brown? fox jumps*over the lazy dog.' print(re.split('; | ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    import re text = 'The quick. black n brown? fox jumps*over the lazy dog.' print(re.split('; | ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
...