Python关于芬德尔()不适用于可选匹配(使用问号)

2024-06-26 14:21:25 发布

您现在位置:Python中文网/ 问答频道 /正文

根据我正在阅读的一个在线教程,上面说:

可选匹配问号

“有时候有一种模式,你只想选择性地匹配。也就是说,正则表达式应该找到一个匹配项,不管这段文本是否存在。这个?字符将其前面的组标记为模式的可选部分。例如,在交互式shell中输入以下内容:“

>>> batRegex = re.compile(r'Bat(wo)?man')
>>> mo1 = batRegex.search('The Adventures of Batman')
>>> mo1.group()
'Batman'

我的问题:

我正在尝试查找匹配的电话号码,格式为123-456-7890(不带国家代码)或{}(有国家代码)。在

下面是python返回匹配电话号码列表的regex代码:

^{pr2}$

然而,我得到的电话号码列表是['', '(933)-', ''],而不是我想要的['800-420-7240, '(933)-415-863-9900', '415-863-9950']。在

我能知道我的代码有什么问题吗?我猜是和“?”有关(可选匹配项)


Tags: 代码标记文本re列表模式电话号码国家
1条回答
网友
1楼 · 发布于 2024-06-26 14:21:25

您将可选部分包括在捕获组中,这意味着re.findall提供给您的就是这些组。在

如果使用非捕获组,则不会发生这种情况。在

re.compile(r'(?:\(\d{3}\)-)?\d{3}-\d{3}-\d{4}')

来自the docs

(...)
Matches whatever regular expression is inside the parentheses, and indicates the start and end of a group; the contents of a group can be retrieved after a match has been performed, and can be matched later in the string with the \number special sequence, described below. To match the literals '(' or ')', use \( or \), or enclose them inside a character class: [(], [)].

(?:...)
A non-capturing version of regular parentheses. Matches whatever regular expression is inside the parentheses, but the substring matched by the group cannot be retrieved after performing a match or referenced later in the pattern.

re.findall(pattern, string, flags=0)
Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result.

(重点是我的)

相关问题 更多 >