André Ikvmo
André Ikvmo

Reputation: 89

How to parse multiple cookies values from Set-Cookie (contains commas and semicolons)

I'm actually using:

(.*?)=(.*?);


But it's not parsing properly due to the commas that it contains.
You can test patterns here > http://gskinner.com/RegExr/

Here's the cookies string:

Set-Cookie: PAGE_VIS=A|186.213.98.144|1381089446.485193|; domain=PAGE; path=/; expires=Mon, 07-Oct-2013 06:00:00 GMT,CSASF=;Version=1;Domain=PAGE;Path=/;Max-Age=7776000,CSAPAGES=ekpEOWluWTZSMXpDSWRMMm8wWWZ3UT09|5749|kNm6h4hlOB4SwLFg2gOIMXXpmTXDD5tnvB/dJ7AS3mUqUiLtpHLxzsWjfKsI1rcq6fp/DKre3QHAZHPQi968iw==;Version=1;Domain=PAGE;Path=/;Max-Age=7776000;HttpOnly,NAM_LOGIN_DEFAULT=user;Version=1;Domain=PAGE;Path=/;Max-Age=129600,UPL09=2-25|39|54|56|58|90|91|105|106|156|182|212|218|219|242|245|248|284|285|480|1222;Version=1;Domain=PAGE;Path=/;Max-Age=2592000,PAGE_OTR=;Version=1;Domain=PAGE;Path=/;Max-Age=2592000,CAUBR01=5eeOUuDdqyj+ILCQ7oOg71yXEgIGi2KJ7bjDiTt27tTXgjT7jqC49eGprgjZ+8rQ7O7mqG/qEq28u4EXAiEbfE0qqI9QqUrj27LrEAi4kSZacSSTAo5YY+yQZYFr4qGgCp18odS/Y92mkEtqEuCoxuG+qsK5fhOJmKQqqpxqE07ORqEn2JGEhOHVmoQqqKCqEq78+2EMqtKZlrvMqv2qmKMqEqqOIqZ72NKGRxO2mKeskKwkZq2CH7GrC+GGoKOCqOVmqEOqEkbr82EjqcGKVHoQqn2qkEMqKk2xLqEk20EEtSr2qo2kqErqEqqODqZh7YKGtxK2mCesmEMsGqVSTMZb24EKuMrTqw2qqEoqEqqK92ZUMnKZsOKqmo2kqEOkEq2KzqKpVvKZlOCMqo7qqGMmKq2oMeEiMXGZBKMqqOQqqKxqZsqv2VK32tEG+Cr7kH2qqEKqEmqCT7G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VtKZTvCMq97kqGMmKquoNeKJMrGZXKMqqvrkqKxqZsqxEVKqqFKGQCx7mKuqsGKqZmevrMZm7wKKdMKVkCqqmEosGqVKLqZecvKGNxouqMesqKtkKqMCv7GbJjZZ1KOqqxVmqK8qGqqMlqKyqLZGIo8esKMqqZKqGk7xFJGuetGK/rKVmoqqkZCsGqbKsqKKVsKZr8CMq87kqGMmKqJoteGYMqGZGKMqq8CkqKxqZsqnzVKdq3EGZCx7mKJqsGKqZme8xMZQ7TGKsMKMkCqqmZosGqVKDqZFThKGxxoJqMesqG4kKqMCo7G7C0ZGFKCqqxVmqK8qGsqM72KSqiZZwo9esKMqqZKqKm7mGVC;Version=1;Domain=PAGE;Path=/

Upvotes: 3

Views: 9472

Answers (3)

Smern
Smern

Reputation: 19066

I think this might work for you:

(.*?)=(.*?)($|;|,(?! ))

the last part is saying:
$ end of string | or ...
; semi-colon | or ...
,(?! ) comma not followed by a space

result

The best thing would be to actually be consistent with your separators (use semi-colons all the time)... and this seems to be kinda hinky but I think it will work for your situation as all the commas you don't want to split at seem to be followed by a space from what I can tell... (as long as your data doesn't change to be like "foo=bar,baz; blah=foo").

Upvotes: 10

Jerry
Jerry

Reputation: 71538

I suggest using a negative lookahead:

(.*?)=(.*?)(?:;|,(?!\s))

The second part of the match will end at a comma which is not followed by a space, and the only comma followed by a space seems to be in the date, while it is being used as separator everywhere else.

Upvotes: 5

Broxzier
Broxzier

Reputation: 2949

The following will split at both a , and a ;, however, I think you shouldn't split at commas! This regex will also match the last entry in the string, since this one is missing a ;.

So you are basically wanting to split on commas and semi-colons instead of just semi-colons? But how would you expect this to work? It seems there are valid cases where a comma isn't meant to split... like "Mon, 07-..." – smerny

(.*?)=((.*?)[;,]|.*$)

Upvotes: 4

Related Questions