Products
GG网络技术分享 2025-03-18 16:15 4
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 ?
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]
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*)
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
今天来写一篇关于正则表达式学习的文章,和大家一起学习正则表达式。
记得有的同事说过这么一句话:”正则表达式对于开发人员来说是最伟大的发明之一”。正则表达式在我们的项目开发中可以说是必不可少的一部分,特别是对我们前端开发人员来说,很多数据需要我们在前端进行格式校验,所以学会正则是很重要的。
首先我们来看一下正则表达式的基本语法:
普通字符:
普通字符包括没有显式指定为元字符的所有可打印和不可打印字符。这包括所有大写和小写字母、所有数字、所有标点符号和一些其他符号。
非打印字符:
\\cx:匹配由x指明的控制字符。例如, \\cM 匹配一个 Control-M 或回车符。x 的值必须为 A-Z 或 a-z 之一。否则,将 c 视为一个原义的 ‘c’ 字符。
\\f:匹配一个换页符。
\\n:匹配一个换行符。
\\r:匹配一个回车符。
\\s:匹配任何空白字符,包括空格、制表符、换页符等等。
\\S:匹配任何非空白字符。
\\t:匹配一个制表符。
\\v:匹配一个垂直制表符。
特殊字符:
$:匹配输入字符串的结尾位置。
( ):标记一个子表达式的开始和结束位置。子表达式可以获取供以后使用。要匹配这些字符,请使用 ‘\\(‘ 和’\\)’。
*:匹配前面的子表达式零次或多次。
+:匹配前面的子表达式一次或多次。
.:匹配除换行符 \\n 之外的任何单字符。
[:标记一个中括号表达式的开始。
?:匹配前面的子表达式零次或一次,或指明一个非贪婪限定符。
\\:将下一个字符标记为或特殊字符、或原义字符、或向后引用、或八进制转义符。
^:匹配输入字符串的开始位置,除非在方括号表达式中使用,此时它表示不接受该字符集合。
{:标记限定符表达式的开始。
|:指明两项之间的一个选择。
限定符:
*:匹配前面的子表达式零次或多次。
+:匹配前面的子表达式一次或多次。
?:匹配前面的子表达式零次或一次,或指明一个非贪婪限定符。
{n}:n 是一个非负整数。匹配确定的 n 次。
{n,}:n 是一个非负整数。至少匹配n 次。
{n,m}:m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。
定位符:
^:匹配输入字符串的开始位置,除非在方括号表达式中使用,此时它表示不接受该字符集合。
$:匹配输入字符串的结尾位置。
\\b:匹配一个字边界,即字与空格间的位置。
\\B:非字边界匹配。
注:不能将限定符与定位点一起使用。由于在紧靠换行或者字边界的前面或后面不能有一个以上位置,因此不允许诸如 ^* 之类的表达式。
明天我将带来正则表达式的实战演练。
欢迎我的微信公众号:「皮蛋菌丶」
Demand feedback