搜尋

首頁  >  問答  >  主體

正規表示式 - python正規符合http包裡cookie問題

麻煩問一下使用python正則或是shell正規如何匹配http包裡面的cookie值

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

需要匹配出
Cookie: PHPSESSID=br5m3mehuvd1kf7hobl3ocdgh5; __utma=48899378.1896211893.1477411078.1477411078.149546489 78.1.10.1495464244; __utmz=48899378.1495464244.2.1.utmcsr=(direct)|utmccn= (direct)|utmcmd=(none); __utmt=1

Cookie([^;]*)(;|$)

我用上面這段這樣只配對出一小段
Cookie: PHPSESSID=br5m3mehuvd1kf7hobl3ocdgh5;
怎麼把所有cookie都配對到
多謝

伊谢尔伦伊谢尔伦2738 天前652

全部回覆(1)我來回復

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-24 11:37:00

    因為你的文字是換行的, 所以直接用.*匹配就好, 因為如果不設定多行模式匹配, 預設是單行

    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)

    回覆
    0
  • 取消回覆