1

I have 0 experience with python, very little with regex and I'm trying to figure out what this small snippet of python regex would give back from a http response header Set-Cookie entry:

REGEX_COOKIE = '([A-Z]+=[^;]+;)'
resp = urllib2.urlopen(req)
re.search(REGEX_COOKIE, resp.info()['Set-Cookie']).group(1)

Can one give a simple example of a Set-Cookie value and explain what this would match on + return?

Regards

1 Answer 1

4

A Set-Cookie is a list of name value pairs, separated with semi-colons:

HTTP/1.1 200 OK
Content-type: text/html
Set-Cookie: RMID=732423sdfs73242; expires=Fri, 31-Dec-2010 23:59:59 GMT

(content of page)

The regular expression matches the name, the equals sign, and the value up to the first semi-colon - in this case: RMID=732423sdfs73242;. In other words, it reads the first name-value pair. Note that it won't work if there is only a single name-value pair which is not followed by a semi-colon.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, clear explanation. If I'm with you, for a Set-Cookie string like this Set-Cookie: RMID=732423sdfs73242; path=/; expires=Fri, 31-Dec-2010 23:59:59 GMT , group(2) would match on RMID=732423sdfs73242; path=/; ?
@nkr1pt: Actually, no it will only find the first. To find all name value pairs you could use re.findall, or even simpler: don't bother with regular expressions, just use str.split(';').

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.