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



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

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)

难道没有更好(不那么冗长(的方法来处理此类异常吗?

您可能会发现此类可用于将大多数 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'

编辑:请参阅此要点以获取此问题的更长讨论(以及替代但类似的解决方案(。

最新更新