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

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. Jmeter (三) 集合点 、检查点 (断言)

    不同的 测试工具有不同的命名 断言 :即检查点,在请求中 加入测试人员的判断,返回 结果 TRUE  or FALSE ,得到 测试人员的判断是否正确 集合点 1.打开  synchronizing ...

  2. web开发中的支付宝支付和微信支付

    https://www.jianshu.com/p/155757d2b9eb <!-- wxPay --SDK--> <script src="https://res.wx ...

  3. Can you answer these queries V SPOJ - GSS5 (分类讨论+线段树维护区间最大子段和)

    recursion有一个整数序列a[n].现在recursion有m次询问,每次她想知道Max { A[i]+A[i+1]+...+A[j] ; x1 <= i <= y1 , x2 &l ...

  4. vs调试时,不显示局部变量

    为了测试一个函数的返回值,就在某个函数里加了一个局部变量,调试却不显示所添加变量的信息. 你一定设置成了release 模式.改为debug就可以了. 比较弱智的问题,mark一下.

  5. mysql中给查询结果添加序号列

    今天同事给了一个小需求,从一个存有不定数量坐标数据的表(map_trace)中每隔20条取一条.最后写了下面这条SQL: select * from (select @n:=@n+1 as n, a. ...

  6. python---win32gui、win32con、win32api:winAPI操作

    python操作winAPI 窗口操作: import sys from PyQt5.QtWidgets import QApplication, QWidget from lianxi import ...

  7. js比较两个时间的大小

    function checkdate(s,e){ //得到日期值并转化成日期格式,replace(/-/g, "//")是根据验证表达式把日期转化成长日期格式,这样再进行判断就好判 ...

  8. Golang ioutil.ReadDir 读取目录下的内容并排序

    之前写的https://www.cnblogs.com/pu369/p/10620731.html一文中,也有对slice的排序,但代码乱的自己也看不下去了. 参考https://blog.csdn. ...

  9. vue 运行脚手架报错

    报错: You are using the runtime-only build of Vue where the template compiler is not available. Either ...

  10. HDU - 6183 暴力,线段树动态开点,cdq分治

    B - Color itHDU - 6183 题目大意:有三种操作,0是清空所有点,1是给点(x,y)涂上颜色c,2是查询满足1<=a<=x,y1<=b<=y2的(a,b)点一 ...