0

i'm trying to extract a value from a Cookie. cookies are organized with [name]=[value] pairs separated by a ';'. so if i have a cookie string like so:

username=user;password=pass;session="4341234556";

in this example, i only want the value '4341234556' without the quotes.

the value of the session variable is, of course, different for each request made. how can i get just the value of session with a regular expression (without the quotes)? keep in mind that the ordering of the variables is not guaranteed to be the same each time it is returned.

also, i would like to ignore any whitespace characters NOT contained in the value.

3
  • 2
    What programming language? What have you tried so far? Commented Feb 6, 2017 at 20:03
  • Is it guaranteed that all those key value pairs exist? Are they all in a single line? Is the value you want to get always enclosed in double quotes, or can it be enclosed in single quotes as well? Commented Feb 6, 2017 at 20:08
  • @GeorgeTs the only K=V pair guaranteed to exist is 'session="stuff"'. yes, they are all received on a single line, but that is not guaranteed either. Commented Feb 6, 2017 at 20:10

1 Answer 1

2

with a regular "consuming" pattern (requires match group access)

session="([^"]+)" //sessionid is in $1

demo

with a lookbehind (entire match is the sessionid, no match groups required):

(?<=session=")[^"]+

demo

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

3 Comments

this worked great (the second solution). however, is there a way to write this such that it will return the same value even if the quotation marks do not exist? [edit: nevermind, i figured that one out. thanks so much!]
if only number, u can replace [^"]+ to \d+
"(?<=session=)"?([^"]+)" (remove quotes) with optional quotes.

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.