正则表达式小疑惑咨询

#(1)

#–coding:UTF-8–

Import re

phoneRegex = re.compile(r”(\d\d\d)-(\d\d\d)-(\d\d\d\d)”)

S = phoneRegex.findall(“111-111-2231”)

print(s)

#输出[(‘111’, ‘111’, ‘2231’)]

#==============================================

#(2)

#–coding:UTF-8–

import re

phoneRegex = re.compile(r”””(

(\d\d\d)-(\d\d\d)-(\d\d\d\d)

)

“””,re.VERBOSE)

s = phoneRegex.findall(“111-111-2231”)

print(s)

#输出 [(‘111-111-2231’, ‘111’, ‘111’, ‘2231’)]

为什么(1)(2)输出的结果不相同,感谢大神指点~

最佳答案

第二个中多了个括号,注意区别

phoneRegex = re.compile(r'((\d\d\d)-(\d\d\d)-(\d\d\d\d))', re.VERBOSE)
phoneRegex = re.compile(r'(\d\d\d)-(\d\d\d)-(\d\d\d\d)', re.VERBOSE)
4年前 评论
讨论数量: 2

第二个中多了个括号,注意区别

phoneRegex = re.compile(r'((\d\d\d)-(\d\d\d)-(\d\d\d\d))', re.VERBOSE)
phoneRegex = re.compile(r'(\d\d\d)-(\d\d\d)-(\d\d\d\d)', re.VERBOSE)
4年前 评论

谢谢!!!~

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!