一、从固定字符串中进行提取
import re
data ='''{'WWW-Authenticate': 'Basic realm="13FFF07B-948F-4654-8DDF-72680B6EE312"', 'Content-Length': '0', 'Date': 'Wed, 07 Aug 2019 06:42:25 GMT'}
'''
str = re.findall(r'realm="(.+?)"', data)
print(str)
二、从返回参数中进行提取
import requests,re,json
url = 'http://www.baidu.com'
data = requests.post(url)#对百度URL进行post请求
result = data.headers#拿到请求返回头
result = json.dumps(str(result))
print(result, type(result))
res = re.findall(''' 'Content-Length': '(.+?)',''', result)
print('res', res)
三、读取文件,从文件中正则提取
文件,新建文本文档(3)中的内容为:
data="songlihui"rurofjr2u9rjnwfu9cp;jfucp8;rnj 8UFHhBfkfFflhFHfFHfhHFKLHlfjHFhfjKFY I1RHLDQyahulqnfpyu8jh ufhlfdata="sunshangxiang"UOU*PG:GPU*:JIOGBT&Y*I)P LOYUFG:H"JKI{GYHKI{PGLIHJOPKE$%^YHBT%R^T&*YU(JIHUdata="ceshibu"data="ceshibu"data="ceshibu" data="ceshibu" 431 ffs fsf
import re
with open(r'C:\Users\songlihui\PycharmProjects\temp\temp_test\新建文本文档 (3).txt', 'r') as f:
s = f.readline()
while s:
res = re.findall(r'data="(.+?)"', s)
if res:
print(res)
s = f.readline()
四、截取一段日志