pythonregex:从regex中包含/删除()或组会在模式匹配中产生差异吗?

2024-06-16 15:00:16 发布

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

我正在尝试匹配存储在某个字符串中的电话号码

str = """ Vijendra contact number is 8861005000 Srinu contact number is 8861005001 pankaj contact number is +91-8861005002 prasad contact number is +92-8861005003"""

对于上面的字符串,我创建了如下正则表达式a=re.compile(r'(+\d+-)?(\d+)')

它能够正确地获取联系人号码

a.findall(str)

[('','8861005000'),('','886100501'),('+91-','886100502'),('+92-','886100503')]

现在我只从regex中删除了(),现在regex看起来像是a=re.compile(r'(+\d+-)?\d+')

这一次,当我试图获取存储在字符串中的联系人号码时,我得到的字符串值只有区号

[“”,“,”+91-“,”+92-“]

为什么new regex无法获取联系人号码?只提取区号

我正在使用Python2.7.12


Tags: 字符串renumberiscontact联系人电话号码regex