leetcode--Python正则表达式解析Valid Phone nums

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fallinlovelj/article/details/54290609
#!/usr/bin/env python
import os
import re
import sys
filename=sys.argv[1]
fd=open(filename,"r")

index=1
for row in fd.readlines() :
    a=re.compile( r"^(?P<region>\d{3}-|\(\d{3}\) )(?P<numes>\d{3})-(?P<tails>\d{4})" )
    b = a.match( row )
    if b:
        print( index  ,b.groups()  )
    index=index+1

example

123-456-7890
(123) 456-7890
123 -
(123 )

注:( )给match.goups(0赋值,如果没有使用()的话,那么goups() 为空
(?Pxxxxx)为指定的规则赋值,之后我们就可以使用b.group(’NAME’)来获取值了

猜你喜欢

转载自blog.csdn.net/fallinlovelj/article/details/54290609