python中的赋值和比较
我需要按顺序重新匹配,在匹配的情况下,我需要匹配结果来选择组。现在我可以做下一步:python中的赋值和比较,python,regex,match,Python,Regex,Match,我需要按顺序重新匹配,在匹配的情况下,我需要匹配结果来选择组。现在我可以做下一步: r = re.match('cond1', l) if r: # work with r.group() else: r = re.match('cond2', l) if r: # work with r.group() else: r = re.match('cond3', l) if r: # work
r = re.match('cond1', l)
if r:
# work with r.group()
else:
r = re.match('cond2', l)
if r:
# work with r.group()
else:
r = re.match('cond3', l)
if r:
# work with r.group()
等等,但我怎样才能做得更好呢?我认为,如果是这样,则无法在中执行分配:
if r = re.match('cond1', l):
# work with r.group()
elif r = re.match('cond2', l):
# work with r.group()
elif r = re.match('cond3', l)
# work with r.group()
你可以用一种理解力:
r, cond = next((m,c) for (m,c) in ((re.match(cond, line), cond) for cond in ('cond1', 'cond2', 'cond3')) if m)
if cond=='cond1':
# work with r.group() for cond1
elif cond=='cond2':
# work with r.group() for cond2
或者,如果这看起来太神秘了,一个循环:
for cond in ('cond1', 'cond2', 'cond3'):
r = re.match(cond, line)
if r:
break
if not r:
# no match
elif cond=='cond1':
# work with r.group() for cond1
elif cond=='cond2':
# work with r.group() for cond2
你可以用一种理解力:
r, cond = next((m,c) for (m,c) in ((re.match(cond, line), cond) for cond in ('cond1', 'cond2', 'cond3')) if m)
if cond=='cond1':
# work with r.group() for cond1
elif cond=='cond2':
# work with r.group() for cond2
或者,如果这看起来太神秘了,一个循环:
for cond in ('cond1', 'cond2', 'cond3'):
r = re.match(cond, line)
if r:
break
if not r:
# no match
elif cond=='cond1':
# work with r.group() for cond1
elif cond=='cond2':
# work with r.group() for cond2
首先,它通常有助于将事物重构成函数。在这种情况下,它很有帮助,因为您可以轻松地从函数中提前返回;不能从其他代码中间的代码块中尽早返回。
def first_match(haystack):
r = re.match('cond1', haystack)
if r:
# work with r.group()
return
r = re.match('cond2', l)
if r:
# work with r.group()
return
r = re.match('cond3', l)
if r:
# work with r.group()
所有的else
位和缩进头痛都消失了
另外,一般来说,当你问如何将3个或更多的东西链接在一起时,正确的答案是找出如何将任意数量的东西链接在一起,只需使用N=3即可。这通常意味着一个循环(或隐藏在函数中的循环,如
map
,或递归函数定义等)。例如:
def first_match(exprs, haystack):
for expr in exprs:
r = re.match(expr, haystack)
if r:
return r
然而,在这种情况下,您试图做的实际上是可行的。也许不是个好主意,但是…正则表达式
匹配对象总是真实的。当然,None
是错误的。因此:
r = re.match('cond1', l) or re.match('cond2', l) or re.match('cond3', l)
if r:
# work with r.group(), which is the group for whichever matched
但请注意,如果要使用真实性,也可以在循环中使用:
next(filter(bool, (re.match(cond, l) for cond in ('cond1', 'cond2', 'cond3'))))
最后,您已经在使用正则表达式了,那么为什么不使用正则表达式呢
r = re.match('cond[123]', l)
if r:
# work with r.group()
首先,它通常有助于将事物重构成函数。在这种情况下,它很有帮助,因为您可以轻松地从函数中提前返回;不能从其他代码中间的代码块中尽早返回。
def first_match(haystack):
r = re.match('cond1', haystack)
if r:
# work with r.group()
return
r = re.match('cond2', l)
if r:
# work with r.group()
return
r = re.match('cond3', l)
if r:
# work with r.group()
所有的else
位和缩进头痛都消失了
另外,一般来说,当你问如何将3个或更多的东西链接在一起时,正确的答案是找出如何将任意数量的东西链接在一起,只需使用N=3即可。这通常意味着一个循环(或隐藏在函数中的循环,如map
,或递归函数定义等)。例如:
def first_match(exprs, haystack):
for expr in exprs:
r = re.match(expr, haystack)
if r:
return r
然而,在这种情况下,您试图做的实际上是可行的。也许不是个好主意,但是…正则表达式匹配对象总是真实的。当然,None
是错误的。因此:
r = re.match('cond1', l) or re.match('cond2', l) or re.match('cond3', l)
if r:
# work with r.group(), which is the group for whichever matched
但请注意,如果要使用真实性,也可以在循环中使用:
next(filter(bool, (re.match(cond, l) for cond in ('cond1', 'cond2', 'cond3'))))
最后,您已经在使用正则表达式了,那么为什么不使用正则表达式呢
r = re.match('cond[123]', l)
if r:
# work with r.group()
不,这是不可能的。是的,我意识到了这一点,但如何更好地处理此类问题?通常,当你试图将3个或更多的东西链接在一起时,最好询问如何将任意数量的东西链接在一起。这通常意味着一个循环(或一个隐式循环,例如,在调用map
,或递归函数中)。此外,如果您真的要匹配这三种模式,为什么不干脆r=re.match('cond[123]”,l)
?您的级联ifs很好(六个月后,当您返回此代码修复错误时,功能性、智能性、正常且易于理解…-)不,这是不可能的。是的,我意识到了这一点,但如何更好地处理此类问题?通常,当您试图将3个或更多的东西链接在一起时,最好询问如何将任意数量的东西链接在一起。这通常意味着一个循环(或隐式循环,例如,在调用map
或递归函数时)另外,如果你真的在匹配这三种模式,为什么不干脆r=re.match('cond[123]',l)
?你的级联ifs很好(当你回到这段代码来修复一个bug时,在六个月内功能、智能、正常且易于理解…-)