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

问题描述 投票:0回答:1

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

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)

难道没有更好的方法(较少冗长)来处理此类异常吗?

python-3.x regex coding-style
1个回答
0
投票

您可能会发现该类对于将大多数if-no-match处理减少到一行非常有用。

© www.soinside.com 2019 - 2024. All rights reserved.