hai im newbie as regex我想先在php中问一些关于regex的东西,我有这样的字符串:
#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
和我的代码是这样的:
$cotext = file_get_contents($cookieFile);
preg_match('/(sessionid)\s[^\S]*/', $cotext, $id);
我想要的就是得到像这样的数组:
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"
你们可以帮我吗?
这会工作
(如OP
注释中所述,字符串sessionid
始终以开头IGSC
)
(sessionid)\s+(IGSC[^\s]+)
记住,[^\s] -> [\S]
。所以你可以随意使用
PHP代码
$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);
你的结果是matches[1]
和matches[2]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句