Reputation: 75
hai im newbie as regex i wanna ask something about regex in php first i have string like this :
#HttpOnly_www.xyz.com FALSE / TRUE 1468386389 sessionid IGSC4c9b34af03c4d6a83e9ac949d5f32a6f9a50ced1362b1797cec54ceac3c57110%3Agp888l8neqxFTtJHXMPWkQfIQ9uKv113%3A%7B%22_token_ver%22%3A2%2C%22_auth_user_id%22%3A2251871689%2C%22_token%22%3A%222251871689%3ApZdi2i1V6EtoR3p7CgADOomNZLxUta74%3A9d2adf87ebeeb84899dba1a383ef822aeb0a9b7d13c50b6052df407f25c676a2%22%2C%22asns%22%3A%7B%222600%3A3c02%3Ae001%3A1a96%3A28ed%3Ab4b%3Ab8bb%3A2c5f%22%3A17025%2C%22time%22%3A1460610396%7D%2C%22_auth_user_backend%22%3A%22accounts.backends.CaseInsensitiveModelBackend%22%2C%22last_refreshed%22%3A1460610395.854883%2C%22_platform%22%3A4%7D www.xyz.com FALSE / FALSE 1460613989 s_network
and my code is like this :
$cotext = file_get_contents($cookieFile);
preg_match('/(sessionid)\s[^\S]*/', $cotext, $id);
all i want is to get array like this :
array [0] => "sessionid"
array [1] => "IGSC4c9b34af03c4d6a83e9ac949d5f32a6f9a50ced1362b1797cec54ceac3c57110%3Agp888l8neqxFTtJHXMPWkQfIQ9uKv113%3A%7B%22_token_ver%22%3A2%2C%22_auth_user_id%22%3A2251871689%2C%22_token%22%3A%222251871689%3ApZdi2i1V6EtoR3p7CgADOomNZLxUta74%3A9d2adf87ebeeb84899dba1a383ef822aeb0a9b7d13c50b6052df407f25c676a2%22%2C%22asns%22%3A%7B%222600%3A3c02%3Ae001%3A1a96%3A28ed%3Ab4b%3Ab8bb%3A2c5f%22%3A17025%2C%22time%22%3A1460610396%7D%2C%22_auth_user_backend%22%3A%22accounts.backends.CaseInsensitiveModelBackend%22%2C%22last_refreshed%22%3A1460610395.854883%2C%22_platform%22%3A4%7D"
can u guys help me ?
Upvotes: 1
Views: 80
Reputation: 1147
You have 2 small errors in your regex. First, your second group needs a "(" so that you can access it.
Second, if you use the \S you don't need a ^. So, it will look like this:
(sessionid)\s+(\S*)
Upvotes: 0
Reputation: 21
Your match pattern string should be
"/(sessionid)\s(\S*)/"
.
1. because \s matches for space, \S matches for non-space. You shouldn't put ^ before \S.
2. You should add parentheses around \S* if you want to catch the sessionid.
so,$id
is now
Array ( [0] => sessionid IGSC4c9b34af03c4d6a83e9ac949d5f32a6f9a50ced1362b1797cec54ce ac3c57110%3Agp888l8neqxFTtJHXMPWkQfIQ9uKv113%3A%7B%22_token_ver%22%3A2%2C%22_aut h_user_id%22%3A2251871689%2C%22_token%22%3A%222251871689%3ApZdi2i1V6EtoR3p7CgADO omNZLxUta74%3A9d2adf87ebeeb84899dba1a383ef822aeb0a9b7d13c50b6052df407f25c676a2%2 2%2C%22asns%22%3A%7B%222600%3A3c02%3Ae001%3A1a96%3A28ed%3Ab4b%3Ab8bb%3A2c5f%22%3 A17025%2C%22time%22%3A1460610396%7D%2C%22_auth_user_backend%22%3A%22accounts.bac kends.CaseInsensitiveModelBackend%22%2C%22last_refreshed%22%3A1460610395.854883% 2C%22_platform%22%3A4%7D [1] => sessionid [2] => IGSC4c9b34af03c4d6a83e9ac949d5f32a6f9a50ced1362b1797cec54ceac3c57110% 3Agp888l8neqxFTtJHXMPWkQfIQ9uKv113%3A%7B%22_token_ver%22%3A2%2C%22_auth_user_id% 22%3A2251871689%2C%22_token%22%3A%222251871689%3ApZdi2i1V6EtoR3p7CgADOomNZLxUta7 4%3A9d2adf87ebeeb84899dba1a383ef822aeb0a9b7d13c50b6052df407f25c676a2%22%2C%22asn s%22%3A%7B%222600%3A3c02%3Ae001%3A1a96%3A28ed%3Ab4b%3Ab8bb%3A2c5f%22%3A17025%2C% 22time%22%3A1460610396%7D%2C%22_auth_user_backend%22%3A%22accounts.backends.Case InsensitiveModelBackend%22%2C%22last_refreshed%22%3A1460610395.854883%2C%22_plat form%22%3A4%7D )
Note that $id[0]
is the whole matching string, while $id[1]
and $id[2]
matches those
parentheses respectively.
Upvotes: 2
Reputation: 11042
This will work
(As OP
mentioned in comments that string after sessionid
always begin with IGSC
)
(sessionid)\s+(IGSC[^\s]+)
Remember, [^\s] -> [\S]
. So you can use any of it as your wish
PHP Code
$re = "/(sessionid)\\s+(IGSC[^\\s]+)/";
$str = "#HttpOnly_www.xyz.com FALSE / TRUE 1468386389 sessionid IGSC4c9b34af03c4d6a83e9ac949d5f32a6f9a50ced1362b1797cec54ceac3c57110%3Agp888l8neqxFTtJHXMPWkQfIQ9uKv113%3A%7B%22_token_ver%22%3A2%2C%22_auth_user_id%22%3A2251871689%2C%22_token%22%3A%222251871689%3ApZdi2i1V6EtoR3p7CgADOomNZLxUta74%3A9d2adf87ebeeb84899dba1a383ef822aeb0a9b7d13c50b6052df407f25c676a2%22%2C%22asns%22%3A%7B%222600%3A3c02%3Ae001%3A1a96%3A28ed%3Ab4b%3Ab8bb%3A2c5f%22%3A17025%2C%22time%22%3A1460610396%7D%2C%22_auth_user_backend%22%3A%22accounts.backends.CaseInsensitiveModelBackend%22%2C%22last_refreshed%22%3A1460610395.854883%2C%22_platform%22%3A4%7D www.xyz.com FALSE / FALSE 1460613989 s_network";
preg_match($re, $str, $matches);
Your result is in matches[1]
and matches[2]
Upvotes: 2