我正在尝试匹配存储在某个字符串中的电话号码
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
您应该看看regex中的非捕获组。 https://docs.python.org/2/howto/regex.html#non-capturing-and-named-groups
如果您使用这些,您可以添加您的组,但它们不会作为结果返回
相关问题 更多 >
编程相关推荐