2015-03-18 62 views
0

我正在尝试编写一个正则表达式来匹配扩展公用日志格式。我有一个表达式来匹配大多数日志条目,但在列出多个主机时会失败。正则表达式与多主机匹配扩展公用日志格式

这是我目前的表现:

([^ ]*) ([^ ]*) ([^ ]*) \[([^]]*)\] "([^"]*)" ([^ ]*) ([^ ]*) "([^"]*)" "([^"]*)" 

此匹配成功的标准日志条目。例如:

24.58.227.240 - - [22/Sep/2011:00:00:00 +0000] "GET /rss/merchant/airsoftpost.com HTTP/1.1" 200 1880 "-" "Apple-PubSub/65" 

然而,一些日志条目包含由逗号分隔的多个主机IP地址:

10.64.233.43, 69.171.229.245 - - [22/Sep/2011:00:00:00 +0000] "GET /view/thesanctuary.co.uk HTTP/1.1" 206 7289 "-" "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)" 

有人能帮助我解决我的表达式匹配任意数量的主机对于给定的日志项?

谢谢。

回答

1

按照你的正则表达式,您可以更改:

([^ ]*) ([^ ]*) ([^ ]*) \[([^]]*)\] "([^"]*)" ([^ ]*) ([^ ]*) "([^"]*)" "([^"]*)" 

([^-]*) ([^ ]*) ([^ ]*) \[([^]]*)\] "([^"]*)" ([^ ]*) ([^ ]*) "([^"]*)" "([^"]*)" 
    ^--- here, match until first dash 

的想法是只改变第一组:

([^ ]*) ---> matches until the first space (change this) 
([^-]*) ---> matches until the first hyphen 
1

作为一个选项,试试这个正则表达式:

([\d.\s,]*) ([^ ]*) ([^ ]*) \[([^]]*)\] "([^"]*)" ([^ ]*) ([^ ]*) "([^"]*)" "([^"]*)"

第一个捕获组现在将捕获所有数字,句点,(白色)空格以及任意数量的重复。

请参阅working demo