我需要从主字符串中提取以下字符串:
roundcube_sessauth=-del-; expires=Thu, 06-Aug-2015 03:38:33 GMT; path=/; secure; httponly
,
roundcube_sessid=dh7a60r14c6qfa3lr7m90; path=/; secure; HttpOnly
,
roundcube_sessauth=S2124929b7486e6805d615a86; path=/; secure; httponly
主要字符串是:
roundcube_sessauth=-del-; expires=Thu, 06-Aug-2015 03:38:33 GMT; path=/; secure; httponly, roundcube_sessid=dh7a60r14c6qfa3lr7m90; path=/; secure; HttpOnly, roundcube_sessauth=S2124929b7486e6805d615a86; path=/; secure; httponly
我的正则表达式是(roundcube.*?httponly){1}
,它与第一个字符串完全匹配。然而,它不匹配第二次或第三次出现,即(roundcube.*?httponly){2}
和(roundcube.*?httponly){3}
请告诉我我做错了什么。我试着用PHP来做这件事。
发布于 2015-08-06 07:07:10
您只使用限制量词{1}
显式地告诉regex引擎匹配1。把它移开,正则表达式就会起作用。
另外,我建议用字界来使它更安全:
\broundcube.*?\bhttponly\b
请参阅演示 (使用不光彩的选项!)
在PHP中,只有拿火柴,而不是组。
$re = '/\broundcube.*?\bhttponly\b/i';
$str = "roundcube_sessauth=-del-; expires=Thu, 06-Aug-2015 03:38:33 GMT; path=/; secure; httponly, roundcube_sessid=dh7a60r14c6qfa3lr7m90; path=/; secure; HttpOnly, roundcube_sessauth=S2124929b7486e6805d615a86; path=/; secure; httponly";
preg_match_all($re, $str, $matches);
print_r($matches[0]);
如果输入中有换行符,则添加/s
修饰符,以便.
也可以匹配换行符。
发布于 2015-08-06 07:07:31
(roundcube[\s\S]*?httponly)
您需要使.
匹配newlines
或使用[\s\S]
instead.Use i
标志too.See演示。
https://regex101.com/r/fM9lY3/22
$re = "/(roundcube[\\s\\S]*?httponly)/mi";
$str = "roundcube_sessauth=-del-; expires=Thu, 06-Aug-2015 03:38:33 GMT; path=/; secure; httponly, roundcube_sessid=dh7a60r14c6qfa3lr7m90; path=/; secure; HttpOnly, roundcube_sessauth=S2124929b7486e6805d615a86; path=/; secure; httponly";
preg_match_all($re, $str, $matches);
https://stackoverflow.com/questions/31848906
复制相似问题