Python 正则表达式:处理不匹配的更好方法?

Python regular expressions: Better way to handle non-matches?

当我处理正则表达式时,我的代码中充斥着条件语句,以便在找不到模式时不会创建异常:

m = some_compiled_pattern.match(s)
if m:
    x = m.groups()
    do_something_with(x)
m = some_other_compiled_pattern.search(s):
if m:
    y = m.groupdict()
else:
    y = {}
do_something_else_with(y)

有没有更好(更简洁)的方法来处理此类异常?

您可能会发现此 class 有助于将大部分 if-no-match 处理减少到一行。

class Returns:
    """
    Makes an object that pretends to have all possible methods, 
    but returns the same value (default None) no matter what this method, 
    or its arguments, is.
    """

    def __init__(self, return_val=None):
        self.return_val = return_val

        def the_only_method_there_is(*args, **kwargs):
            return return_val

        self.the_only_method_there_is = MethodType(the_only_method_there_is, self)

    def __getattr__(self, item):
        if not item.startswith('_') and item not in {'return_val', 'the_only_method_there_id'}:
            return self.the_only_method_there_is
        else:
            return getattr(self, item)

使用示例:

    >>> import re
    >>> p = re.compile(r'(\d+)\W+(\w+)')
    >>>
    >>> # when all goes well...
    >>> m = p.search('The number 42 is mentioned often')
    >>> num, next_word = m.groups()
    >>> num, next_word
    ('42', 'is')
    >>>
    >>> # when the pattern is not found...
    >>> m = p.search('No number here')
    >>> assert m is None  # m is None so...
    >>> num, next_word = m.groups()  # ... this is going to choke
    Traceback (most recent call last):
      ...
    AttributeError: 'NoneType' object has no attribute 'groups'
    >>>
    >>> # Returns to the rescue
    >>> num, next_word = (p.search('No number here') or Returns((None, 'default_word'))).groups()
    >>> assert num is None
    >>> next_word
    'default_word'

编辑:请参阅 this gist 了解有关此问题的更长时间的讨论(以及替代但类似的解决方案)。