Python正则表达式前向/后向搜索的肯定/否定模式的区别和示例

零宽断言区别

 含义语法示例
前向搜索肯定模式
零宽度正预测先行断言
匹配exp前面的位置(?=exp)\b\w+(?=ing\b)查找I'm singing while you're dancing.匹配到sing danc
前向搜索否定模式
零宽度负预测先行断言
匹配后面跟的不是exp的位置(?!exp)\d{3}(?!\d)匹配三位数字,而且这三位数字的后面不能是数字;
\b((?!abc)\w)+\b匹配不包含连续字符串abc的单词
后向搜索肯定模式
零宽度正回顾后发断言
匹配exp后面的位置(?<=exp)(?<=\bre)\w+\b查找reading a book得到ading
((?<=\d)\d{3})+\b查找1234567890得到234567890
(?<=<(\w+)>).*(?=<\/\1>)匹配不包含属性的简单HTML标签内里的内容
后向搜索否定模式
零宽度负回顾后发断言
匹配前面不是exp的位置(?<!exp)(?<![a-z])\d{7}匹配前面不是小写字母的七位数字

他们只匹配一个位置,并不消费任何字符。
<表示把零宽度(预查)放到要匹配的表达式前面,不带就放到后面。
!表示非,不需要的意思。

前向搜索肯定模式例子

# -*-coding:utf-8-*-

import re 
  
address = re.compile(u'((?P<name>([\w.,]+\s+)*[\w.,]+)\s+)(?=(<.*>$)|([^<].*[^>]$))<?(?P<email>[\w\d.+-]+@([\w\d.]+\.)+(com|org|edu))>?', 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 u'Candidate:', candidate
    match = address.search(candidate)
    if match:  
        print u'  Name :', match.groupdict()['name']
        print u'  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

前向搜索否定模式例子

# -*-coding:utf-8-*-

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:', u'first.last@example.com')
('  Match:', u'first.last@example.com')
('Candidate:', u'noreply@example.com')
  No match

后向搜索否定模式例子

# -*-coding:utf-8-*-

import re

pattern = u'^[\w\d\.+-]+(?<!noreply)@([\w\d.]+\.)+(com|org|edu)$'
ls = [u'first.last@example.com', u'noreply@example.com']

for txt in ls:
    print 'Candidate:', txt
    match = re.search(pattern, txt)
    if match:
        print u'    Match:', match.group(0)
    else:
        print u'    No match'

输出结果:

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

后向搜索肯定模式例子

# -*-coding:utf-8-*-

import re

pattern = re.compile('(?<=@)([\w\d_]+)', re.VERBOSE)
text = '''This text includes two Twitter handles. 
One for @caimouse, and one for the author, @caijunsheng. 
'''

print text
for match in pattern.findall(text):
    print match

输出:

This text includes two Twitter handles.
One for @caimouse, and one for the author, @caijunsheng.

caimouse
caijunsheng

参考

  • http://deerchao.net/tutorials/regex/regex.htm
  • http://blog.csdn.net/caimouse/article/details/78481084
  • 7
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小龙在山东

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值