python如何使用正则表达式的前向、后向搜索及前向搜索否定模式详解


Posted in Python onNovember 08, 2017

前言

在许多的情况下,很多要匹配内容是一起出现,或者一起不出现的。比如《》,< >,这样的括号,不存在使用半个的情况。因此,在正则表达式里也有一致性的判断,要么两个尖括号一起出现,要么一个也不要出现。怎么样来实现这种判断呢?针对这种情况得引入新的正则表达式的语法:(?=pattern),这个语法它会向前搜索或者向后搜索相关内容,如果不会出现就不能匹配。不过,这个匹配不会消耗任何输入的字符,它只是查看一下。

例子如下:

#python 3.6 
#蔡军生 
#http://blog.csdn.net/caimouse/article/details/51749579 
# 
import re 
 
address = re.compile( 
 ''''' 
 # A name is made up of letters, and may include "." 
 # for title abbreviations and middle initials. 
 ((?P<name> 
  ([\w.,]+\s+)*[\w.,]+ 
  ) 
  \s+ 
 ) # name is no longer optional 
 
 # LOOKAHEAD 
 # Email addresses are wrapped in angle brackets, but only 
 # if both are present or neither is. 
 (?= (<.*>$)  # remainder wrapped in angle brackets 
  | 
  ([^<].*[^>]$) # remainder *not* wrapped in angle brackets 
  ) 
 
 <? # optional opening angle bracket 
 
 # The address itself: username@domain.tld 
 (?P<email> 
  [\w\d.+-]+  # username 
  @ 
  ([\w\d.]+\.)+ # domain name prefix 
  (com|org|edu) # limit the allowed top-level domains 
 ) 
 
 >? # optional closing angle bracket 
 ''', 
 re.VERBOSE) 
 
candidates = [ 
 u'First Last <first.last@example.com>', 
 u'No Brackets first.last@example.com', 
 u'Open Bracket <first.last@example.com', 
 u'Close Bracket first.last@example.com>', 
] 
 
for candidate in candidates: 
 print('Candidate:', candidate) 
 match = address.search(candidate) 
 if match: 
  print(' Name :', match.groupdict()['name']) 
  print(' Email:', match.groupdict()['email']) 
 else: 
  print(' No match')

结果输出如下:

Candidate: First Last <first.last@example.com>
 Name : First Last
 Email: first.last@example.com
Candidate: No Brackets first.last@example.com
 Name : No Brackets
 Email: first.last@example.com
Candidate: Open Bracket <first.last@example.com
 No match
Candidate: Close Bracket first.last@example.com>
 No match

python里使用正则表达式的前向搜索否定模式

上面学习前向搜索或后向搜索模式(?=pattern),这个模式里看到有等于号=,它是表示一定相等,其实前向搜索模式里,还有不相等的判断。比如你需要识别EMAIL地址:noreply@example.com,这个EMAIL地址大多数是不需要回复的,所以我们要把这个EMAIL地址识别出来,并且丢掉它。怎么办呢?这时你就需要使用前向搜索否定模式,它的语法是这样:(?!pattern),这里的感叹号就是表示非,不需要的意思。比如遇到这样的字符串:noreply@example.com,它会判断noreply@是否相同,如果相同,就丢掉这个模式识别,不再匹配。

例子如下:

#python 3.6 
#蔡军生 
#http://blog.csdn.net/caimouse/article/details/51749579 
# 
import re 
 
address = re.compile( 
 ''''' 
 ^ 
 
 # An address: username@domain.tld 
 
 # Ignore noreply addresses 
 (?!noreply@.*$) 
 
 [\w\d.+-]+  # username 
 @ 
 ([\w\d.]+\.)+ # domain name prefix 
 (com|org|edu) # limit the allowed top-level domains 
 
 $ 
 ''', 
 re.VERBOSE) 
 
candidates = [ 
 u'first.last@example.com', 
 u'noreply@example.com', 
] 
 
for candidate in candidates: 
 print('Candidate:', candidate) 
 match = address.search(candidate) 
 if match: 
  print(' Match:', candidate[match.start():match.end()]) 
 else: 
  print(' No match')

结果输出如下:

Candidate: first.last@example.com
 Match: first.last@example.com
Candidate: noreply@example.com
 No match

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对三水点靠木的支持。

Python 相关文章推荐
Python列表(list)、字典(dict)、字符串(string)基本操作小结
Nov 28 Python
python3+mysql查询数据并通过邮件群发excel附件
Feb 24 Python
深入分析python数据挖掘 Json结构分析
Apr 21 Python
python多进程实现文件下载传输功能
Jul 28 Python
详解python解压压缩包的五种方法
Jul 05 Python
Python实现某论坛自动签到功能
Aug 20 Python
python hash每次调用结果不同的原因
Nov 21 Python
pytorch使用horovod多gpu训练的实现
Sep 09 Python
python 两种方法删除空文件夹
Sep 29 Python
python 利用opencv实现图像网络传输
Nov 12 Python
python实战之用emoji表情生成文字
May 08 Python
Python入门之三角函数全解【收藏】
Nov 08 #Python
Python入门之三角函数tan()函数实例详解
Nov 08 #Python
Python入门之三角函数sin()函数实例详解
Nov 08 #Python
Python入门之三角函数atan2()函数详解
Nov 08 #Python
使用Pyinstaller的最新踩坑实战记录
Nov 08 #Python
python的变量与赋值详细分析
Nov 08 #Python
浅谈python迭代器
Nov 08 #Python
You might like
PHP 中的一些经验积累
2006/10/09 PHP
解决File size limit exceeded 错误的方法
2013/06/14 PHP
php简单防盗链实现方法
2015/07/29 PHP
PHP+mysql+ajax轻量级聊天室实现方法详解
2016/10/17 PHP
bindParam和bindValue的区别以及在Yii2中的使用详解
2018/03/12 PHP
PHPUnit测试私有属性和方法功能示例
2018/06/12 PHP
laravel异步监控定时调度器实例详解
2019/06/21 PHP
通过修改referer下载文件的方法
2008/05/11 Javascript
firefo xml 读写实现js代码
2009/06/11 Javascript
二叉树先序遍历的非递归算法具体实现
2014/01/09 Javascript
Jquery节点遍历next与nextAll方法使用示例
2014/07/22 Javascript
HTML5之WebSocket入门3 -通信模型socket.io
2015/08/21 Javascript
BootStrap下拉菜单和滚动监听插件实现代码
2016/09/26 Javascript
js中json处理总结之JSON.parse
2016/10/14 Javascript
AngularJS指令与控制器之间的交互功能示例
2016/12/14 Javascript
js瀑布流布局的实现
2020/06/28 Javascript
[31:01]2014 DOTA2国际邀请赛中国区预选赛5.21 CNB VS Orenda
2014/05/23 DOTA
[40:03]Liquid vs Optic 2018国际邀请赛淘汰赛BO3 第一场 8.21
2018/08/22 DOTA
python里大整数相乘相关技巧指南
2014/09/12 Python
python中urllib.unquote乱码的原因与解决方法
2017/04/24 Python
Python实现图片批量加入水印代码实例
2019/11/30 Python
基于python纯函数实现井字棋游戏
2020/05/27 Python
python转化excel数字日期为标准日期操作
2020/07/14 Python
欧舒丹加拿大官网:L’Occitane加拿大
2017/10/29 全球购物
GoDaddy英国:全球排名第一的域名注册商
2018/06/08 全球购物
商务助理岗位职责
2013/11/13 职场文书
自荐信怎么写呢?
2013/12/09 职场文书
房产销售经理职责
2013/12/20 职场文书
学校四群教育实施方案
2014/06/12 职场文书
企业安全生产责任书范本
2014/07/28 职场文书
2015年环卫工作总结
2015/04/28 职场文书
超市主管竞聘书
2015/09/15 职场文书
《春酒》教学反思
2016/02/22 职场文书
《童年》读后感(三篇)
2019/08/27 职场文书
基于PyQt5制作一个群发邮件工具
2022/04/08 Python
Python中time标准库的使用教程
2022/04/13 Python