在python中以串行方式检查具有许多正则表达式的字符串时,会有太多缩进

时间:2012-05-21 18:33:06

标签: python conditional indentation

当我编写如下代码

时,我会深陷缩进
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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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!')

答案 2 :(得分: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语句中,它将继续进行下一次测试。

答案 3 :(得分:0)

我在另一个线程中找到了一个相关的答案,该线程使用一个类来保存数据以模拟C中的赋值条件习惯用法

https://stackoverflow.com/a/1806338/1408564