问题之后会在下面列出HTTP消息。
127.0.0.1 - Johny [17/Dec/2010:17:15:16 -0700] "GET /apache_pb.gif
HTTP/1.0" 200 2326
127.0.0.1 - debbie7 [19/Dec/2010:11:11:02 -0700] "GET /apache_pbs.gif
HTTP/1.0" 404 2336
谢谢!
答案 0 :(得分:2)
您可以使用此正则表达式在一次传递中提取值{username,date和http code}:
^.*?-\s(\S*)\s+\[([^\]]*)\]\s"[^"]*"\s(\d+)\s\d+
组0获取整行,而其他组将分别获得相应的匹配。
你没有选择一种语言,所以我提供了一个php示例来展示正则表达式是如何工作的
给定输入字符串,在消息区域的中间完成链接中断
127.0.0.1 - Johny [17/Dec/2010:17:15:16 -0700] "GET /apache_pb.gif
HTTP/1.0" 200 2326
127.0.0.1 - debbie7 [19/Dec/2010:11:11:02 -0700] "GET /apache_pbs.gif
HTTP/1.0" 404 2336
代码示例
<?php
$sourcestring="your source string";
preg_match_all('/^.*?-\s(\S*)\s+\[([^\]]*)\]\s"[^"]*"\s(\d+)\s\d+/im',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>
$matches Array:
(
[0] => Array
(
[0] => 127.0.0.1 - Johny [17/Dec/2010:17:15:16 -0700] "GET /apache_pb.gif
HTTP/1.0" 200 2326
[1] => 127.0.0.1 - debbie7 [19/Dec/2010:11:11:02 -0700] "GET /apache_pbs.gif
HTTP/1.0" 404 2336
)
[1] => Array
(
[0] => Johny
[1] => debbie7
)
[2] => Array
(
[0] => 17/Dec/2010:17:15:16 -0700
[1] => 19/Dec/2010:11:11:02 -0700
)
[3] => Array
(
[0] => 200
[1] => 404
)
)
答案 1 :(得分:0)
HTTP状态:
(?<=HTTP/1.0" )\d+
请求用户(适用于任何IP地址):
(?<=(\d\d?\d?\.){3}\d\d?\d? - )\w+(?= \[)
时间戳:
(?<=\[).*(?=\])
答案 2 :(得分:0)
您可以尝试使用此Regex来实现此目的:
^.* (\w*) \[([^\]]*)] \"[\w.\/ ]*\" ([\d]+)
<强>输入:强>
127.0.0.1 - Johny [17/Dec/2010:17:15:16 -0700] "GET /apache_pb.gif
HTTP/1.0" 200 2326
<强>输出:强>
Group 1: Johny
Group 2: 17/Dec/2010:17:15:16 -0700
Group 3: 200
您可以测试正则表达式here。
答案 3 :(得分:0)
Perl:
!([a-zA-Z]+) \W+
(.* -) [\w\W]+
HTTP/1.0" \ ([\d]+)
!x
$1 -> username
$2 -> timestamp
$3 -> status