python如何使用正則表達式的前向、后向搜索及前向搜索否定模式詳解
前言
在許多的情況下,很多要匹配內(nèi)容是一起出現(xiàn),或者一起不出現(xiàn)的。比如《》,< >,這樣的括號,不存在使用半個的情況。因此,在正則表達式里也有一致性的判斷,要么兩個尖括號一起出現(xiàn),要么一個也不要出現(xiàn)。怎么樣來實現(xiàn)這種判斷呢?針對這種情況得引入新的正則表達式的語法:(?=pattern),這個語法它會向前搜索或者向后搜索相關內(nèi)容,如果不會出現(xiàn)就不能匹配。不過,這個匹配不會消耗任何輸入的字符,它只是查看一下。
例子如下:
#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地址大多數(shù)是不需要回復的,所以我們要把這個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
總結
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關文章
Python基于pyecharts實現(xiàn)關聯(lián)圖繪制
這篇文章主要介紹了Python基于pyecharts實現(xiàn)關聯(lián)圖繪制,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03
用Python獲取攝像頭并實時控制人臉的實現(xiàn)示例
這篇文章主要介紹了用Python獲取攝像頭并實時控制人臉的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07
解決使用Pycharm導入conda?environment時找不到python.exe
今天在使用conda創(chuàng)建環(huán)境之后,使用pycham發(fā)現(xiàn)找到自己的python環(huán)境但是找不到環(huán)境對應的python.exe,這篇文章主要給大家介紹了關于如何解決使用Pycharm導入conda?environment時找不到python.exe的相關資料,需要的朋友可以參考下2023-10-10
Python中調(diào)用和運行其他.py文件的多種實現(xiàn)方法
本文介紹了在Python中調(diào)用和運行其他.py文件的四種方法:subprocess模塊、exec函數(shù)、import語句和os.system函數(shù),每種方法都有其適用場景和優(yōu)缺點2025-02-02

