[英]PHP regex matching multiple entries?
I'm parsing a log file line by line. 我正在逐行解析日志文件。
I want to match on the following : 我想在以下方面进行匹配:
"GET /manager/ HTTP/1.1" 200
"GET /manager HTTP/1.1" 200
"GET /manager HTTP/1.1" 401
"GET /manager/ HTTP/1.1" 401
Currently I'm assigning each of these options to it's own variable, then doing : 目前,我将每个选项分配给它自己的变量,然后执行以下操作:
if (strpos($line,$a) || strpos($line,$b) || strpos($line,$c) || strpos($line,$d))
{
}
if there a better way ? 是否有更好的方法? thanks
谢谢
This regular expression will match all four variations: 此正则表达式将匹配所有四个变体:
^"GET /manager/? HTTP/1\.1" (200|401)$
You will also have to escape the slashes with a backslash unless you use another character as the delimiter. 除非您使用另一个字符作为分隔符,否则还必须使用反斜杠来转义斜杠。
Try it 试试吧
$str = '
"GET /manager/ HTTP/1.1" 200
"GET /manager HTTP/1.1" 200
"GET /manager HTTP/1.1" 401
"GET /manager/ HTTP/1.1" 401
';
preg_match_all('#^\s*("GET\s+/manager/?\s+HTTP/1\.1"\s+(?:200|401)+)#im', $str, $match);
print_r($match[1]);
Try this regex (GET|POST)\\s+(.*)\\s+(HTTP.*)\\s+(\\d{3})
This will give you ["GET", "/manager/", "HTTP/1.1", "401"]
试试这个正则表达式
(GET|POST)\\s+(.*)\\s+(HTTP.*)\\s+(\\d{3})
这将为您提供["GET", "/manager/", "HTTP/1.1", "401"]
Updated code: 更新的代码:
$logStr = '"GET /manager/ HTTP/1.1" 200
"GET /manager HTTP/1.1" 200
"GET /manager HTTP/1.1" 401
"GET /manager/ HTTP/1.1" 401
';
echo $logStr;
preg_match('/(GET|POST)\s+(.*)\s+(HTTP.*)\s+(\d{3})/', $logStr, $matches);
print_r($matches);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.