一个正则表达式的问题
程序代码:
phoneRegex = (r'''( (\d{3}|\(\d{3}\))? (\s|-|.)? (\d{3}) (\s|-|.) (\d{4}) (\s*(ext|x|ext\.)\s*(\d{2,5}))? )''', re.VERBOSE)
书上的关于正则表达式找电话号码的例题,第八组是分机号,但是这里面哪个是第八组?只有六组吧?
phoneRegex = (r'''( (\d{3}|\(\d{3}\))? (\s|-|.)? (\d{3}) (\s|-|.) (\d{4}) (\s*(ext|x|ext\.)\s*(\d{2,5}))? )''', re.VERBOSE)
>>> m=phoneRegex.match('222-444-666ext88') >>> print(m) None >>> m=phoneRegex.match('222-444-6666ext88') >>> print(m) <_sre.SRE_Match object; span=(0, 17), match='222-444-6666ext88'> >>> print(m.group(0)) 222-444-6666ext88 >>> print(m.group(1)) 222-444-6666ext88 >>> print(m.group(2)) 222 >>> print(m.group(3)) - >>> print(m.group(4)) 444 >>> print(m.group(5)) - >>> print(m.group(6)) 6666 >>> print(m.group(7)) ext88 >>> print(m.group(8)) ext >>> print(m.group(9)) 88