零宽断言 python_正则表达式-零宽断言
[toc]
一、零寬斷言-介紹
零寬斷言,它匹配的內容不會提取,其作用是在一個限定位置的字符串向前或向后進行匹配查找。
1.1、應用場景
排除查找,查找不含有某段字符串的行
包含查找,查找含有某段字符串的行
二、斷言的分類
2.1、正先行斷言
什么是正先行斷言,就是在字符串相應位置之前進行查找匹配,使用 (?=exp) 匹配exp前面的位置。
import re
str = 'abcgwcab'
pattern = 'bc(?=gw)'
result = re.search(pattern,str)
print(result.group())
# 輸出結果
bc
解析:首先查找字符串”abcgwcab”中gw位置,斷言為真,然后再匹配 bc,然后再向后匹配。
example:
pattern = 'bc(?=gw)ca'
# 匹配失敗,因為找到了 gw 的位置后,斷言為真,再向前匹配 bc ,再然后是從 bc 處進行匹配是 gwca ,所以會失敗。
pattern = 'bc(?=gw)gwca'
# 匹配成功,輸出結果
bcgwca
2.2、反先行斷言
什么是反先行斷言,使用 (?!exp) 匹配后面跟的不是exp。
import re
str = 'abcgwcab'
pattern = 'bc(?!ww)gw'
result = re.search(pattern,str)
print(result.group())
# 輸出結果
bcgw
解析:首先判斷字符串是否包含bc,然后判斷其后面不是ww,斷言為真,然后從 bc 處進行匹配 gw。
2.3、正后發斷言
什么是正后發斷言,就是在字符串相應位置之后進行查找匹配, (?<=exp) 匹配exp后面的位置
import re
str = 'abcgwcab'
pattern = '(?<=gw)ca'
result = re.search(pattern,str)
print(result.group())
# 輸出結果
ca
解析:首先判斷字符串是否包含 gw ,然后查找后面是否有 ca,存在,斷言為真,則從 ca 處開始繼續匹配。
example:
import re
str = 'abcgwcab'
pattern = 'gw(?<=gw)cab'
result = re.search(pattern,str)
print(result.group())
# 輸出結果
gwcab
2.4、反后發斷言
什么是反后發斷言,就是在給定位置的字符串向前查找,(?
import re
str = 'abcgwcab'
pattern = '(?
result = re.search(pattern,str)
print(result.group())
# 輸出結果
False
解析:首先查找字符串中是否包含 gw ,然后判斷 gw 前面是不是 bc ,如果是則返回 False。 如果不是,則返回 True,然后從 gw 處開始匹配。
example:
import re
str = 'abcgwcab'
pattern = 'gw(?
result = re.search(pattern,str)
print(result.group())
# 輸出結果
gwca
'''
在字符串中查找 ca ,然后判斷其前面是不是 bc ,不是,返回 True ,然后從 ca 處開始匹配,匹配到 gw 。 則輸出為 gwca
'''
三、排除查找
3.1、查找不以 baidu 開頭的字符串
源文本
baidu.com
sina.com.cn
代碼
import re
source_str = 'baidu.com\nsina.com.cn'
str_list = source_str.split('\n')
print(str_list)
for str in str_list:
pattern = '^(?!baidu).*$'
result = re.search(pattern,str)
if result:
print(result.group())
# 輸出結果
sina.com.cn
解析:^(?!baidu).*$ 從行首開始匹配,查找后面不是 baidu 的字符串。(?!baidu) 這段是反先行斷言
3.2、查找不以 com 結尾的字符串
源文本
baidu.com
sina.com.cn
www.educ.org
www.hao.cc
www.redhat.com
代碼
import re
source_str = 'baidu.com\nsina.com.cn\nwww.educ.org\nwww.hao.cc\nwww.redhat.com'
str_list = source_str.split('\n')
print(str_list)
# ['baidu.com', 'sina.com.cn', 'www.educ.org', 'www.hao.cc', 'www.redhat.com']
for str in str_list:
pattern = '^.*?(?
result = re.search(pattern,str)
if result:
print(result.group())
# 輸出結果
sina.com.cn
www.educ.org
www.hao.cc
解析:'^.?(?
3.3、查找文本中不含有 world 的行
源文本
I hope the world will be peaceful
Thepeoplestheworldoverlovepeace
Imissyoueveryday
Aroundtheworldin80Days
I usually eat eggs at breakfast
代碼
import re
source_str = 'I hope the world will be peaceful\nThepeoplestheworldoverlovepeace\nImissyoueveryday\nAroundtheworldin80Days\nI usually eat eggs at breakfast'
str_list = source_str.split('\n')
print(str_list)
# ['I hope the world will be peaceful', 'Thepeoplestheworldoverlovepeace', 'Imissyoueveryday', 'Aroundtheworldin80Days', 'I usually eat eggs at breakfast']
for str in str_list:
pattern = '^(?!.*world).*$'
result = re.search(pattern,str)
if result:
print(result.group())
# 輸出結果
Imissyoueveryday
I usually eat eggs at breakfast
解析:^ 首先匹配行首,(?!.*world) , 匹配行首后不能有 .*world 的字符, 也就是不能有 xxxxxxxworld 的字符。 這就排除了從行首開始后面有 world 字符的情況了。
四、實戰操作
4.1、日志匹配(一)
從日志文件中過濾 [ERROR] 的錯誤日志,但錯誤日志又分兩種,一種是帶 _eMsg 參數的,一種是不帶的。
需求是過濾出所有的錯誤日志,但排除 _eMsg=400 的行。
源文本
[ERROR][2020-04-02T10:27:05.370+0800][clojure.fn__147.core.clj:1] _com_im_error||traceid=ac85e854d7600001b6970||spanid=8a0a0084||cspanid=||serviceName=||errormsg=get-driver-online-status timeou||_eMsg=Read timed out||_eTrace=java.net.SocketTimeoutException: Read timed out
[ERROR][2020-04-02T10:30:17.353+0800][clojure.fn__147.core.clj:1] _com_im_error||traceid=0f05e854e38984b3f1f20||spanid=8a980083||cspanid=||serviceName=||errormsg=Handle request failed||_eMsg=400 Bad Request||_eTrace=sprin.web.Exception$BadRequest: 400 Bad Request
[ERROR][2020-03-25T09:21:16.186+0800][spring.util.HttpPoolClientUtil] http get error
代碼
import re
source_str = '[ERROR][2020-04-02T10:27:05.370+0800][clojure.fn__147.core.clj:1] _com_im_error||traceid=ac85e854d7600001b6970||spanid=8a0a0084||cspanid=||serviceName=||errormsg=get-driver-online-status timeou||_eMsg=Read timed out||_eTrace=java.net.SocketTimeoutException: Read timed out\
\n[ERROR][2020-04-02T10:30:17.353+0800][clojure.fn__147.core.clj:1] _com_im_error||traceid=0f05e854e38984b3f1f20||spanid=8a980083||cspanid=||serviceName=||errormsg=Handle request failed||_eMsg=400 Bad Request||_eTrace=sprin.web.Exception$BadRequest: 400 Bad Request\
\n[ERROR][2020-03-25T09:21:16.186+0800][spring.util.HttpPoolClientUtil]'
str_list = source_str.split('\n')
# print(str_list)
for str in str_list:
pattern = '(^\[ERROR\].*?_eMsg(?!=400).*$)|^\[ERROR\](?!.*_eMsg).*'
result = re.search(pattern,str)
if result:
print(result.group())
# 輸出結果
[ERROR][2020-04-02T10:27:05.370+0800][clojure.fn__147.core.clj:1] _com_im_error||traceid=ac85e854d7600001b6970||spanid=8a0a0084||cspanid=||serviceName=||errormsg=get-driver-online-status timeou||_eMsg=Read timed out||_eTrace=java.net.SocketTimeoutException: Read timed out
[ERROR][2020-03-25T09:21:16.186+0800][spring.util.HttpPoolClientUtil]
解析:(^\[ERROR\].*?_eMsg(?!=400).*$) 從行首匹配 [ERROR] ,.*? 忽略優先,優先忽略不匹配的任何字符。_eMsg(?!=400) 找到 _eMsg 字符串,匹配其后面是不是 =400 如果是返回 False。
之后 | 或邏輯符,^\[ERROR\](?!.*_eMsg).* 從行首匹配 [ERROR] ,然后匹配出不包含 xxxxxx_eMsg 的行。
寫后面那串或邏輯的目的是為了匹配出,不包含 _eMsg 字段的錯誤日志。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的零宽断言 python_正则表达式-零宽断言的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 蛮力法在求解凸包问题中的应用(JAVA)
- 下一篇: python2.7 安装numpy no