gpt4 book ai didi

Python 正则表达式组

转载 作者:太空宇宙 更新时间:2023-11-03 16:23:23 25 4
gpt4 key购买 nike

为什么这个正则表达式会打印 ('c',)()?我认为“([abc])+”===“([abc])([abc])([abc])...”

>>> import re
>>> m = re.match("([abc])+", "abc")
>>> print m.groups()
('c',)
>>> m.groups(0)
('c',)
>>> m = re.match("[abc]+", "abc")
>>> m.groups()
()
>>> m.groups(0)
()

最佳答案

来自有关的文档 groups

Return a tuple containing all the subgroups of the match, from 1 up to however many groups are in the pattern. The default argument is used for groups that did not participate in the match; it defaults to None.

在第一个正则表达式([abc])+中,它匹配字符abc 但只会存储最后一场比赛

([abc])+
<----->
Matches a or b or c
Observe carefully. Capturing groups are surrounding only the character class
So, only one character from the matched character class can be stored in capturing group.

如果您想在捕获组中捕获字符串abc,请使用

([abc]+)

Above will find string composed of a or b or c and will store it in capturing group.

在第二个正则表达式 [abc]+ 中,没有捕获组,因此显示空结果。

关于Python 正则表达式组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38216055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com