Python中的正则表达式
来源:互联网 发布:hive 数据倾斜 编辑:程序博客网 时间:2024/05/19 12:23
import repattern = "^\d{3}\-\d{3,8}$"phone0 = "010-66101234"phone1 = "010-123"phone2 = "010-12345"print("result is %s" % re.match(pattern, phone0))print("result is %s" % re.match(pattern, phone1))print("result is %s" % re.match(pattern, phone2))pattern = "^(\d{3})\-(\d{3,8}$)"m = re.match(pattern, phone0)print(m.lastindex)i = 0while i <= m.lastindex: print("group %s is %s" % (i, m.group(i))) i = i + 1telephone = re.compile(pattern)print(telephone.match(phone0).groups())
result is <_sre.SRE_Match object; span=(0, 12), match=’010-66101234’>
result is <_sre.SRE_Match object; span=(0, 7), match=’010-123’>
result is <_sre.SRE_Match object; span=(0, 9), match=’010-12345’>
2
group 0 is 010-66101234
group 1 is 010
group 2 is 66101234
(‘010’, ‘66101234’)
阅读全文