views:

731

answers:

2

Hi, The following code is visual basic, .NET, ASP... all of the above? Can anybody tell me what these two "Regex.Replace" functions/methods will leave me with when "strCookies" is equal to the block of text below the two Regex.Replace functions? I believe the idea is simply to capture the "sessionid" code and then the "cadata" code with or without the title/key of "sessionid" and "cadata". Ultimatly I'm trying to do the equivelent in php...the problem is I'm trying to deduce what I need to actually have exactly in the end based on this code. Hope that makes sense. Thanks! (I also wanted to add that for the "cadata" code, I'm specifically confused as to whether I'm suppose to be grabbing the double quotes surrounding the whole code. The sessionid doesn't have and double quotes so it's less confusing. I can't tell what the Regex.Replace function is doing).


Dim strCADataCookie As String = Regex.Replace(strCookies, "(.)sessionid=(.)(,|;)(.*)", "$2")

Dim strSessionIDCookie As String = Regex.Replace(strCookies, "(.)cadata=""(.)""(.*)", "$2")


Netscape HTTP Cookie File http://www.netscape.com/newsref/std/cookie_spec.html This file was generated by libcurl! Edit at your own risk.

a.test.edu FALSE / FALSE 0 sessionid ca391d4e-69ad-4726-b00c-efa26f3d1594

a.test.edu FALSE / TRUE 0 cadata "2YUj33BRih9xeQqCL1PLAY+0EExSdDkGJdQg7KoUbca6OQxrgJi0AuuPBOEhCu7wlG2+2in5ilEE="

A: 

Hi, I run the code you showed in your question and for given strCookie example the result is strCookie for both strCADataCookie and strSessionIDCookie. The reason is that no replace is made because the pattern is not matched in the strCookie.

You may read some info about regular expressions here: http://www.regular-expressions.info/reference.html

The regular expressions from your example are a bit odd. Everything that would be matched by either pattern would be replaced by single character. $2 - refers to the value of second expression in the bracket."" - means ", first " is just an escape character.

Hope this will help you.

empi
+1  A: 

Regex.Replace(strCookies, "(.)sessionid=(.)(,|;)(.*)", "$2")

That returns the value of a cookie named sessionid.

Regex.Replace(strCookies, "(.)cadata=""(.)""(.*)", "$2")

That returns the value of a cookie named cadata after unquoting it. So the resulting string would be stored without the quotes.

Ken Browning