8行python代码正则表达式实现电话号码按格式筛选

前段时间公司培训一个小题目(很简单),大概要求是读取TXT,按要求的格式,筛选出正确的电话号码,如data.txt中保存了如下的几行

987-123-4567
123 456 7890
(123) 456-7890

现在电话号码格式为:(xxx) xxx-xxxx或xxx-xxx-xxxx,读取TXT输出正确的号码

下面给出两种解法:

#coding=utf-8  
import re
def setPhoneNumber():
    phoneDate=[]
    listRightFirst=[1,2,3,6,7,8,10,11,12,13]
    listRightSecond=[0,1,2,4,5,6,8,9,10,11]
    list_sp = [" ","-","(",")"]
    i=True
    j=True
    phoneLines = open(r"D:\eclipse_python\eclipse_workplace\2333\src\data.txt","r")
    for phoneLine in phoneLines:
        phoneLine = phoneLine.strip('\n')
        if  phoneLine[0] == "(" and phoneLine[4]==")" and phoneLine[5]==" " and phoneLine[9]=="-":
            for x in listRightFirst:
                if phoneLine[x]  in list_sp :
                    i=False 
                    break
            if i == True:
                phoneDate.append(phoneLine)                 
        if phoneLine[3]=="-" and phoneLine[7]=="-":
            for y in listRightSecond:
                if phoneLine[y] in list_sp:
                    j=False
                    break
            if j==True:
                phoneDate.append(phoneLine)
        i=True 
        j=True
        
    print phoneDate
if __name__=="__main__":
    setPhoneNumber()

题目很简单,代码也很简单,就不解释了。

后面想到已知了格式,为什么不直接正则匹配呢,于是有了如下的代码:

#coding=utf-8  
import re
def setPhoneNumber():
    phoneLines = open(r"D:\eclipse_python\eclipse_workplace\2333\src\data.txt","r")
    for phoneLine in phoneLines:
        phoneLine = phoneLine.strip('\n')
        if re.findall("(\d{3})\-(\d{3})\-(\d{4})", phoneLine):
            print phoneLine
        if re.findall("\((\d{3})\)\s(\d{3})\-(\d{4})", phoneLine):
            print phoneLine
if __name__=="__main__":
    setPhoneNumber()
  

简单的练习,当做熟悉正则了。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值