python正则表达式-复杂表达式必须用预编译

在正则表达式中,采用预编译的优势就是可以节约时间,提高效率,但是在一些复杂的正则表达式中,如果不采用预编译会导致处理不过来而导致查询失败。
例如查询邮箱。

# -*- coding: utf-8 -*-
import re
import re
Emailstr="""

    caoxigang@baidu.com
曹 艳    Caoyan    6895    13811661805    caoyan@baidu.com
曹 宇    Yu Cao    8366    13911404565    caoyu@baidu.com
曹 越    Shirley Cao    6519    13683604090    caoyue@baidu.com
曹 政    Cao Zheng    8290    13718160690    caozheng@baidu.com
查玲莉    Zha Lingli    6259    13552551952    zhalingli@baidu.com
查 杉    Zha Shan    8580    13811691291    zhashan@baidu.com
查 宇    Rachel    8825    13341012971    zhayu@baidu.com
柴桥子    John    6262    13141498105    chaiqiaozi@baidu.com
常丽莉    lily    6190    13661003657    changlili@baidu.com
车承轩    Che Chengxuan    6358    13810729040    chechengxuan@baidu.com
陈 洁    Che    13811696984    chenxi_cs@baidu.com
陈 超    allen    8391    13810707562    chenchao@baidu.com
陈朝辉        13714189826    chenchaohui@baidu.com
陈 辰    Chen Chen    6729    13126735289    chenchen_qa@baidu.com
陈 枫    windy    8361    13601365213    chenfeng@baidu.com
陈海腾    Chen Haiteng    8684    13911884480    chenhaiteng@baidu.com
陈 红    Hebe    8614    13581610652    chenhong@baidu.com
陈后猛    Chen Houmeng    8238    13811753474    chenhoumeng@baidu.com
陈健军    Chen Jianjun    8692    13910828583    chenjianjun@baidu.com
陈 景    Chen Jing    6227    13366069932    chenjing@baidu.com
陈竞凯    Chen Jingkai    6511    13911087971    jchen@baidu.com
陈 坤    Isa13810136756    chenlei@baidu.com
陈 林    Lin Chen    6828    13520364278    chenlin@baidu.com

"""
searchobj=re.findall("([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4})",Emailstr)
print(searchobj) #复杂的表达式,必须预编译

运行结果为:

在这里插入图片描述
加上预编译以后

regexmail=re.compile("([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4})",re.IGNORECASE)#忽略异常
mailist=regexmail.findall(Emailstr)
print(mailist)

运行结果为:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值