hai im newbie as regex我想在php中询问有关正则表达式的一些内容 首先我有这样的字符串:
#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"
你能帮助我吗?
答案 0 :(得分:2)
这将有效
(在评论中提及OP
,sessionid
之后的字符串始终以IGSC
开头)
(sessionid)\s+(IGSC[^\s]+)
<强> Regex Demo 强>
记住,[^\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]
<强> Ideone Demo 强>
答案 1 :(得分:2)
你的匹配模式字符串应该是
"/(sessionid)\s(\S*)/"
。
1.因为\ s匹配空格,\ S匹配非空格。你不应该在\ S之前放^。
2.如果要捕获sessionid,则应在\ S *周围添加括号。
所以,$id
现在是
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 )
请注意$id[0]
是完整的匹配字符串,$id[1]
和$id[2]
匹配
括号分别。
答案 2 :(得分:0)
你的正则表达式中有2个小错误。首先,你的第二组需要一个&#34;(&#34;以便你可以访问它 其次,如果您使用\ S,则不需要^。所以,它看起来像这样:
(sessionid)\s+(\S*)