Rumah > Soal Jawab > teks badan
Sila tanya cara menggunakan python regular atau shell regular untuk memadankan nilai cookie dalam pakej http
GET /common_img/info/infomenu_08.gif HTTP/1.1
Host: baidu.com
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:43.0) Gecko/20100101 Firefox/43.0 Iceweasel/43.0.4
Accept: image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://baidu.com/files/ie/commonEtc.css
Cookie: PHPSESSID=br5m3mehuvd1kf7hobl3ocdgh5; __utma=48899378.1896211893.1477411078.1477411078.1495464244.2; __utmc=48899378; __utmb=48899378.1.10.1495464244; __utmz=48899378.1495464244.2.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmt=1
Connection: keep-alive
If-Modified-Since: Mon, 16 Dec 2013 04:12:09 GMT
If-None-Match: "7c0978-e63-3f7d840"
key=%D1
Perlu padankan Cookie: PHPSESSID=br5m3mehuvd1kf7hobl3ocdgh5; __utma=48899378.1896211893.1477411078.1477411078.1495464244.2; __utmc=48899378; __utmb=48899378.1.10.1495464244; __utmz=48899378.1495464244.2.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmt=1
Cookie([^;]*)(;|$)
Saya menggunakan perenggan di atas untuk memadankan bahagian kecil sahaja
Kuki: PHPSESSID=br5m3mehuvd1kf7hobl3ocdgh5;
Cara memadankan semua kuki
Terima kasih
曾经蜡笔没有小新2017-05-24 11:37:00
Oleh kerana teks anda dibalut baris, hanya gunakan .*
padanan terus, kerana jika anda tidak menetapkan padanan corak berbilang baris, lalainya ialah satu baris
import re
a = '''GET /common_img/info/infomenu_08.gif HTTP/1.1
Host: baidu.com
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:43.0) Gecko/20100101 Firefox/43.0 Iceweasel/43.0.4
Accept: image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://baidu.com/files/ie/commonEtc.css
Cookie: PHPSESSID=br5m3mehuvd1kf7hobl3ocdgh5; __utma=48899378.1896211893.1477411078.1477411078.1495464244.2; __utmc=48899378; __utmb=48899378.1.10.1495464244; __utmz=48899378.1495464244.2.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmt=1
Connection: keep-alive
If-Modified-Since: Mon, 16 Dec 2013 04:12:09 GMT
If-None-Match: "7c0978-e63-3f7d840"
key=%D1'''
print re.findall(r'(Cookie.*)', a)