关于前向,后向,匹配,非匹配

Matches if ... matches next, but doesn’t consume any of the string. This is called a lookahead assertion. For example, Isaac (?=Asimov) will match 'Isaac ' only if it’s followed by 'Asimov'.(?!...)Matches if ... doesn’t match next. This is a negative lookahead assertion. For example, Isaac (?!Asimov) will match 'Isaac ' only if it’s not followed by 'Asimov'.(?<=...)Matches if the current position in the string is preceded by a match for ... that ends at the current position. This is called a positive lookbehind assertion. (?<=abc)def will find a match in abcdef, since the lookbehind will back up 3 characters and check if the contained pattern matches. The contained pattern must only match strings of some fixed length, meaning that abc or a|b are allowed, but a* and a{3,4} are not. Note that patterns which start with positive lookbehind assertions will never match at the beginning of the string being searched; you will most likely want to use the search() function rather than the match() function:

>>> import re
>>> m = re.search('(?<=abc)def', 'abcdef')
>>> m.group(0)
'def'

This example looks for a word following a hyphen:

>>> m = re.search('(?<=-)\w+', 'spam-egg')
>>> m.group(0)
'egg'
(?<!...)Matches if the current position in the string is not preceded by a match for .... This is called a negative lookbehind assertion. Similar to positive lookbehind assertions, the contained pattern must only match strings of some fixed length. Patterns which start with negative lookbehind assertions may match at the beginning of the string being searched.

前向,后向,匹配,非匹配示例代码:

 import re
def testPrevPostMatch():
# post match: (?=xxx)
# post non-match: (?!xxx)
# prev match: (?<=xxx)
# prev non-match: (?<!xxx) #note that input string is:
#src=\"http://b101.photo.store.qq.com/psb?/V10ppwxs00XiXU/5dbOIlYaLYVPWOz*1nHYeSFq09Z5rys72RIJszCsWV8!/b/YYUOOzy3HQAAYqsTPjz7HQAA\"
qqPicUrlStr = 'src=\\"http://b101.photo.store.qq.com/psb?/V10ppwxs00XiXU/5dbOIlYaLYVPWOz*1nHYeSFq09Z5rys72RIJszCsWV8!/b/YYUOOzy3HQAAYqsTPjz7HQAA\\"'
qqPicUrlInvalidPrevStr = '1234567http://b101.photo.store.qq.com/psb?/V10ppwxs00XiXU/5dbOIlYaLYVPWOz*1nHYeSFq09Z5rys72RIJszCsWV8!/b/YYUOOzy3HQAAYqsTPjz7HQAA\\"'
qqPicUrlInvalidPostStr = 'src=\\"http://b101.photo.store.qq.com/psb?/V10ppwxs00XiXU/5dbOIlYaLYVPWOz*1nHYeSFq09Z5rys72RIJszCsWV8!/b/YYUOOzy3HQAAYqsTPjz7HQAA123'
canFindPrevPostP = r'(?<=src=\\")(?P<qqPicUrl>http://.+?\.qq\.com.+?)(?=\\")'
qqPicUrl = "" foundPrevPost = re.search(canFindPrevPostP, qqPicUrlStr)
print "foundPrevPost=",foundPrevPost
if(foundPrevPost):
qqPicUrl = foundPrevPost.group("qqPicUrl")
print "qqPicUrl=",qqPicUrl; # qqPicUrl= http://b101.photo.store.qq.com/psb?/V10ppwxs00XiXU/5dbOIlYaLYVPWOz*1nHYeSFq09Z5rys72RIJszCsWV8!/b/YYUOOzy3HQAAYqsTPjz7HQAA
print "can found qqPicUrl here" foundInvalidPrev = re.search(canFindPrevPostP, qqPicUrlInvalidPrevStr)
print "foundInvalidPrev=",foundInvalidPrev; # foundInvalidPrev= None
if(not foundInvalidPrev):
print "can NOT found qqPicUrl here" foundInvalidPost = re.search(canFindPrevPostP, qqPicUrlInvalidPostStr)
print "foundInvalidPost=",foundInvalidPost; # foundInvalidPost= None
if(not foundInvalidPost):
print "can NOT found qqPicUrl here" return

Python中正则表达式关于引用named group的用法示例

 import re
def testBackReference():
# back reference (?P=name) test
backrefValidStr = '"group":0,"iconType":"NonEmptyDocumentFolder","id":"9A8B8BF501A38A36!601","itemType":32,"name":"released","ownerCid":"9A8B8BF501A38A36"'
backrefInvalidStr = '"group":0,"iconType":"NonEmptyDocumentFolder","id":"9A8B8BF501A38A36!601","itemType":32,"name":"released","ownerCid":"987654321ABCDEFG"'
backrefP = r'"group":\d+,"iconType":"\w+","id":"(?P<userId>\w+)!\d+","itemType":\d+,"name":".+?","ownerCid":"(?P=userId)"'
userId = "" foundBackref = re.search(backrefP, backrefValidStr)
print "foundBackref=",foundBackref; # foundBackref= <_sre.SRE_Match object at 0x02B96660>
if(foundBackref):
userId = foundBackref.group("userId")
print "userId=",userId; # userId= 9A8B8BF501A38A36
print "can found userId here" foundBackref = re.search(backrefP, backrefInvalidStr)
print "foundBackref=",foundBackref; # foundBackref= None
if(not foundBackref):
print "can NOT found userId here" return

Python 正则表达式【二】的更多相关文章

  1. python正则表达式二[转]

    原文:http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html 1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一 ...

  2. python3.4学习笔记(十二) python正则表达式的使用,使用pyspider匹配输出带.html结尾的URL

    python3.4学习笔记(十二) python正则表达式的使用,使用pyspider匹配输出带.html结尾的URL实战例子:使用pyspider匹配输出带.html结尾的URL:@config(a ...

  3. Python正则表达式初识(二)

    前几天给大家分享了Python正则表达式初识(一),介绍了正则表达式中的三个特殊字符“^”.“.”和“*”,感兴趣的伙伴可以戳进去看看,今天小编继续给大家分享Python正则表达式相关特殊字符知识点. ...

  4. Python 正则表达式入门(中级篇)

    Python 正则表达式入门(中级篇) 初级篇链接:http://www.cnblogs.com/chuxiuhong/p/5885073.html 上一篇我们说在这一篇里,我们会介绍子表达式,向前向 ...

  5. python正则表达式 小例几则

    会用到的语法 正则字符 释义 举例 + 前面元素至少出现一次 ab+:ab.abbbb 等 * 前面元素出现0次或多次 ab*:a.ab.abb 等 ? 匹配前面的一次或0次 Ab?: A.Ab 等 ...

  6. Python 正则表达式-OK

    Python正则表达式入门 一. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一部分. 正则表达式是用于处理字符串的强大工具, 拥有自己独特的语法以及一个独立的处理引擎, 效率上 ...

  7. Python正则表达式一

    推荐 http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html#!comments 这篇博客超好,建议收藏. 不过对于正则表达式小白,他没 ...

  8. python 正则表达式汇总

    一. 正则表达式基础 1.1.概念介绍 正则表达式是用于处理字符串的强大工具,它并不是Python的一部分. 其他编程语言中也有正则表达式的概念,区别只在于不同的编程语言实现支持的语法数量不同. 它拥 ...

  9. 一篇搞定Python正则表达式

    1. 正则表达式语法 1.1 字符与字符类 1 特殊字符:\.^$?+*{}[]()| 以上特殊字符要想使用字面值,必须使用\进行转义 2 字符类    1. 包含在[]中的一个或者多个字符被称为字符 ...

  10. Python正则表达式很难?一篇文章搞定他,不是我吹!

    1. 正则表达式语法 1.1 字符与字符类 1 特殊字符:.^$?+*{}| 以上特殊字符要想使用字面值,必须使用进行转义 2 字符类 1. 包含在[]中的一个或者多个字符被称为字符类,字符类在匹配时 ...

随机推荐

  1. Ubuntu18.04安装 NVIDIA驱动

    Ubuntu18.04安装 NVIDIA驱动 参考自博客:https://blog.csdn.net/jsjason1/article/details/88086904 我确定这篇文章是否很有必要,我 ...

  2. Beta冲刺版本第一天

    该作业所属课程:https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2 作业要求地址:https://edu.cnblogs.com ...

  3. Selenium(2)

    testing123456peter123456rose123456 一.常见的运行错误: 1.[error] Timed out after 30000ms Timed out:超时 after:之 ...

  4. js页面重定向跳转代码总结(待续)

    情形一:东八区,浏览器中文跳转 <script type="text/javascript"> var sLang = (navigator.language ? na ...

  5. 在RecyclerView中集成QQ汽泡二

    上次已经将GooView集成到RecyclerView当中了[http://www.cnblogs.com/webor2006/p/7787511.html],但是目前还有很多问题,下面先来运行看一下 ...

  6. loj2314 「NOIP2017」小凯的疑惑[同余最短路or数论]

    这题以前就被灌输了“打表找规律”的思想,所以一直没有好好想这道题,过了一年还不太会qwq.虽然好像确实很简单,但是还是带着感觉会被嘲讽的心态写这个题解...而且还有一个log做法不会... 法1:(一 ...

  7. ARDUINO UNO烧录BOOTLOADER

    批量烧录为了速度加快,使用USBASP工具,配合PROGISP软件进行烧录. 因为脱离了ARDUINO IDE,所以需要研究AVR单片机的熔丝位设置问题. 刷ATMEGA32U4芯片,需要这样设置: ...

  8. HTML5上传超大文件解决方案

    一.概述 所谓断点续传,其实只是指下载,也就是要从文件已经下载的地方开始继续下载.在以前版本的HTTP协议是不支持断点的,HTTP/1.1开始就支持了.一般断点下载时才用到Range和Content- ...

  9. Ubuntu:笔记本计算机屏幕合盖后进入/不进入休眠设置

    造冰箱的大熊猫,本文在Ubuntu 18.04上验证通过@cnblogs 2019/5/12 1.问题描述 安装有Ubuntu的笔记本计算机与KVM控制器相连,同时一台台式机也与KVM相连.启动笔记本 ...

  10. monkeyrunner操作多个设备的例子

    # -*- coding: utf-8 -*-   from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice from com.an ...