【发布时间】:2021-02-24 07:29:36
【问题描述】:
我的代码:
import re
#Phone Number regex
phoneRegex = re.compile(r'''(
(\d{3}|\(\d{3}\))?
(\s|-|\.)? # separator
(\d{3}) # first 3 digits
(\s|-|\.) # separator
(\d{4}) # last 4 digits
(\s*(ext|x|ext.)\s*(\d{2,5}))? # extension
)''', re.VERBOSE)
phoneRegex.findall('Phone: 800.420.7240 or +1 415.863.9900 (9 a.m. to 5 p.m., M-F, PST)')
输出:
[('800.420.7240', '800', '.', '420', '.', '7240', '', '', ''), ('415.863.9900', '415', '.', '863', '.', '9900', '', '', '')]
问题:
- 为什么匹配中包含空字符串?
- 从字符串的哪些位置匹配空字符串?
- 空字符串匹配的条件是什么?
附言 当我在 https://regex101.com/ 上使用相同的正则表达式时,匹配中不包含空字符串 另外,我几天前才开始学习正则表达式,如果我的问题不够好,我很抱歉。
【问题讨论】:
标签: python python-3.x regex