[当我处理正则表达式时,我的代码中充斥着条件语句,以便在找不到模式时不创建异常:
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处理减少到一行非常有用。