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的包管理器pip更换软件源的方法详解
Jun 20 Python
python 类详解及简单实例
Mar 24 Python
Python操作csv文件实例详解
Jul 31 Python
解决Matplotlib图表不能在Pycharm中显示的问题
May 24 Python
python2与python3共存问题的解决方法
Sep 18 Python
python 并发编程 多路复用IO模型详解
Aug 20 Python
用sqlalchemy构建Django连接池的实例
Aug 29 Python
TensorFlow Saver:保存和读取模型参数.ckpt实例
Feb 10 Python
Python多线程:主线程等待所有子线程结束代码
Apr 25 Python
python如何删除列为空的行
Jul 17 Python
Python绘图之二维图与三维图详解
Aug 04 Python
DRF使用simple JWT身份验证的实现
Jan 14 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
overlord人气高涨,却被菲利普频繁举报,第四季很难在国内上映
2020/05/06 日漫
Phpbean路由转发的php代码
2008/01/10 PHP
php网站来路获取代码(针对搜索引擎)
2010/06/08 PHP
PHP实现股票趋势图和柱形图
2015/02/07 PHP
php添加数据到xml文件的简单例子
2016/09/08 PHP
JQuery 前台切换网站的样式实现
2009/06/22 Javascript
Tab页界面,用jQuery及Ajax技术实现
2009/09/21 Javascript
jq选项卡鼠标延迟的插件实例
2013/05/13 Javascript
JS localStorage实现本地缓存的方法
2013/06/22 Javascript
Javascript 修改String 对象 增加去除空格功能(示例代码)
2013/11/30 Javascript
extjs 时间范围选择自动判断的实现代码
2014/06/24 Javascript
js实现鼠标悬浮给图片加边框的方法
2015/01/30 Javascript
js完美解决IE6不支持position:fixed的bug
2015/04/24 Javascript
Bootstrap Metronic完全响应式管理模板之菜单栏学习笔记
2016/07/08 Javascript
jq实现左滑显示删除按钮,点击删除实现删除数据功能(推荐)
2016/08/23 Javascript
NodeJS模块与ES6模块系统语法及注意点详解
2019/01/04 NodeJs
vue element-ui之怎么封装一个自己的组件的详解
2019/05/20 Javascript
Vue使用富文本编辑器Vue-Quill-Editor(含图片自定义上传服务、清除复制粘贴样式等)
2020/05/15 Javascript
[05:08]DOTA2-DPC中国联赛3月6日Recap集锦
2021/03/11 DOTA
python实现文件分组复制到不同目录的例子
2014/06/04 Python
详解Django通用视图中的函数包装
2015/07/21 Python
详解Python中open()函数指定文件打开方式的用法
2016/06/04 Python
详解python中的 is 操作符
2017/12/26 Python
使用python自动追踪你的快递(物流推送邮箱)
2020/03/17 Python
Python-jenkins模块获取jobs的执行状态操作
2020/05/12 Python
详解Python 函数参数的拆解
2020/09/02 Python
python中strip(),lstrip(),rstrip()函数的使用讲解
2020/11/17 Python
利用HTML5中Geolocation获取地理位置调用Google Map API在Google Map上定位
2013/01/23 HTML / CSS
高中毕业生的个人自我评价
2014/02/21 职场文书
大龄毕业生求职别忘职业规划
2014/03/11 职场文书
业务内勤岗位职责
2014/04/30 职场文书
暑期培训班招生方案
2014/08/26 职场文书
业务员工作态度散漫检讨书
2014/11/02 职场文书
2016幼儿园教师年度考核评语
2015/12/01 职场文书
怎样写好工作计划
2019/04/10 职场文书
利用Python判断你的密码难度等级
2021/06/02 Python