Home  >  Q&A  >  body text

Regular expression - python regular matching cookie problem in http package

Please ask how to use python regular or shell regular to match the cookie value in the http package

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

Need to match
Cookie: PHPSESSID=br5m3mehuvd1kf7hobl3ocdgh5; __utma=48899378.1896211893.1477411078.1477411078.1495464244.2; __utmc=48899378; __utmb=488 99378.1.10.1495464244; __utmz=48899378.1495464244.2.1.utmcsr=(direct)|utmccn= (direct)|utmcmd=(none); __utmt=1

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

I use the above paragraph to only match a small section
Cookie: PHPSESSID=br5m3mehuvd1kf7hobl3ocdgh5;
How to match all cookies
Thank you

伊谢尔伦伊谢尔伦2705 days ago631

reply all(1)I'll reply

  • 曾经蜡笔没有小新

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

    Because your text is line-wrapped, just use .*matching directly, because if you don’t set multi-line pattern matching, the default is single-line

    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)

    reply
    0
  • Cancelreply