3

当我编写如下代码时,我得到了深深的缩进

match = re.search(some_regex_1, s)
if match:
    # do something with match data
else:
    match = re.search(some_regex_2, s)
    if match:
        # do something with match data
    else:
        match = re.search(soem_regex_3, s)
        if match:
            # do something with match data
        else:
            # ...
            # and so on

我试图重写为:

if match = re.search(some_regex_1, s):
    # ...
elif match = re.search(some_regex_2, s):
    # ...
elif ....
    # ...
...

但 Python 不允许这种语法。在这种情况下,我应该怎么做才能避免深度压痕?

4

3 回答 3

6
regexes = (regex1, regex2, regex3)
for regex in regexes:
    match = re.search(regex, s)
    if match:
        #do stuff
        break

或者(更高级):

def process1(match_obj):
    #handle match 1

def process2(match_obj):
    #handle match 2

def process3(match_obj):
    #handle match 3
.
.
.
handler_map = ((regex1, process1), (regex2, process2), (regex3, process3))
for regex, handler in handler_map:
    match = re.search(regex, s)
    if match:
        result = handler(match)
        break
else:
    #else condition if no regex matches
于 2012-05-21T18:38:06.740 回答
2

如果您可以使用finditer()而不是search()(大多数时候可以),您可以将所有正则表达式合并为一个并使用符号组名称。这是一个例子:

import re

regex = """
   (?P<number> \d+ ) |
   (?P<word> \w+ ) |
   (?P<punctuation> \. | \! | \? | \, | \; | \: ) |
   (?P<whitespace> \s+ ) |
   (?P<eof> $ ) |
   (?P<error> \S )
"""

scan = re.compile(pattern=regex, flags=re.VERBOSE).finditer

for match in scan('Hi, my name is Joe. I am 1 programmer.'):
    token_type = match.lastgroup
    if token_type == 'number':
        print 'found number "%s"' % match.group()
    elif token_type == 'word':
        print 'found word "%s"' % match.group()
    elif token_type == 'punctuation':
        print 'found punctuation character "%s"' % match.group()
    elif token_type == 'whitespace':
        print 'found whitespace'
    elif token_type == 'eof':
        print 'done parsing'
        break
    else:
        raise ValueError('String kaputt!')
于 2012-05-21T19:15:50.220 回答
0
if re.search(some_regex_1, s) is not None:
    # ...
elif re.search(some_regex_2, s) is not None:
    # ...
elif ....
    # ...
...

如果没有找到匹配项,search() 将返回 None,因此在您的 if 语句中它将继续进行下一个测试。

于 2012-05-21T18:40:38.867 回答