英文:
Assignment expression in a while loop
问题
m.end()
是指前一个匹配 m
的结束位置,因此搜索从前一个匹配的结束位置开始。我阅读了 PEP-572,其中有一节专门讨论了正则表达式匹配,但没有涉及到 while
循环。我理解 m
在循环体内是可用的,但这不是你提出的问题。
英文:
m = re.match(pattern, text)
while m := p.search(text, m.end()):
# do with match
Does the m.end()
refer to the previous match m
so that the search starts from the end of the previous match? I read PEP-572, and there's a section that talks specifically about regex matches, but not with a while
loop. I understand that m
is available in the body of the loop, that's not the question here.
答案1
得分: 0
这段代码假设文本始终以匹配项开头,否则 while
语句会引发类型错误,因为 re.match
总是尝试从开头匹配,不像 re.search
会搜索整个字符串(当然,除非你使用锚点)。
所以是的,它基本上从上一次匹配位置的末尾开始不断搜索,找到每个后续的非重叠匹配。你可以像这样看到这个过程:
>>> import re
>>> text = "foo some other stuff foo again foo foofofoo"
>>> p = re.compile("foo")
>>> m = p.match(s) # 假定它总是从开头匹配
>>> while print(m) or (m:= p.search(s, m.end())):
... pass
...
<re.Match object; span=(0, 3), match='foo'>
<re.Match object; span=(21, 24), match='foo'>
<re.Match object; span=(31, 34), match='foo'>
<re.Match object; span=(35, 38), match='foo'>
<re.Match object; span=(40, 43), match='foo'>
因此,while 循环的实际主体将只访问从第二个匹配项开始的每个匹配项。
请注意,在这种情况下,你可能只想使用 .finditer
和一个 for 循环:
>>> for m in p.finditer(text):
... print(m)
...
<re.Match object; span=(0, 3), match='foo'>
<re.Match object; span=(21, 24), match='foo'>
<re.Match object; span=(31, 34), match='foo'>
<re.Match object; span=(35, 38), match='foo'>
<re.Match object; span=(40, 43), match='foo'>
除非主体在更新 m
。请注意,在这种情况下,for 循环将访问第一个匹配项,与 while 循环的主体不同。如果你想跳过第一个匹配项,你可以这样做:
>>> it = p.finditer(text)
>>> next(it)
<re.Match object; span=(0, 3), match='foo'>
>>> for m in it:
... print(m)
...
<re.Match object; span=(21, 24), match='foo'>
<re.Match object; span=(31, 34), match='foo'>
<re.Match object; span=(35, 38), match='foo'>
<re.Match object; span=(40, 43), match='foo'>
英文:
This code assumes the text will always start with a match, otherwise, the while
cause would raise a type error, since re.match
always tries to match at the beginning, unlike re.search
which will search the whole string (unless of course you use an anchor).
So yes, it basically keeps searching from the end of the last matching position, finding each subsequent non-overlapping match. You can see this happen like this:
>>> import re
>>> text = "foo some other stuff foo again foo foofofoo"
>>> p = re.compile("foo")
>>> m = p.match(s) # assumes it always matches at the beginning
>>> while print(m) or (m:= p.search(s, m.end())):
... pass
...
<re.Match object; span=(0, 3), match='foo'>
<re.Match object; span=(21, 24), match='foo'>
<re.Match object; span=(31, 34), match='foo'>
<re.Match object; span=(35, 38), match='foo'>
<re.Match object; span=(40, 43), match='foo'>
So the actual body of the while-loop will only have access to every match starting with the second match.
Note that in this case, you probably just want to use .finditer
and a for-loop
>>> for m in p.finditer(text):
... print(m)
...
<re.Match object; span=(0, 3), match='foo'>
<re.Match object; span=(21, 24), match='foo'>
<re.Match object; span=(31, 34), match='foo'>
<re.Match object; span=(35, 38), match='foo'>
<re.Match object; span=(40, 43), match='foo'>
Unless the body is updating m
. Note, in this case, the for-loop would have access to the first match, unlike the body of the while loop. if you wanted to skip the first one, you could always do:
>>> it = p.finditer(text)
>>> next(it)
<re.Match object; span=(0, 3), match='foo'>
>>> for m in it:
... print(m)
...
<re.Match object; span=(21, 24), match='foo'>
<re.Match object; span=(31, 34), match='foo'>
<re.Match object; span=(35, 38), match='foo'>
<re.Match object; span=(40, 43), match
答案2
得分: 0
OP在这里。我进行了一个小实验,证明了我的猜想。
def do(x):
print(f'在do中的x = {x}')
return (x + 1) if x < 5 else None
x = 0
while (x := do(x)) is not None:
print(f'在while中的x = {x}')
输出:
在do中的x = 0
在while中的x = 1
在do中的x = 1
在while中的x = 2
在do中的x = 2
在while中的x = 3
在do中的x = 3
在while中的x = 4
在do中的x = 4
在while中的x = 5
在do中的x = 5
因此,对我的问题的答案是:“是的,在while
循环中,即使在退出循环体后,变量的先前值仍然可用”。实际上,就像普通变量一样,赋值变量在定义点之后的函数中仍然处于作用域内。
英文:
OP here. I did a small experiment that proved what I was after.
def do(x):
print(f'x in do = {x}')
return (x + 1) if x < 5 else None
x = 0
while (x := do(x)) is not None:
print(f'x in while = {x}')
Output:
x in do = 0
x in while = 1
x in do = 1
x in while = 2
x in do = 2
x in while = 3
x in do = 3
x in while = 4
x in do = 4
x in while = 5
x in do = 5
Thus, the answer to my question is "yes, the previous value of the variable is available to the while
loop even after the body is exited". Actually, just like a regular variable, the assignment variable is in scope for the rest of the function from the point it is defined.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论