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

来源:互联网 发布:软件如何创建快捷方式 编辑:程序博客网 时间:2024/06/06 01:28
#!/usr/bin/env pythonimport osimport reimport sysfilename=sys.argv[1]fd=open(filename,"r")index=1for 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-7890123 -(123 )

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

1 0
原创粉丝点击