-4

这是表达式:

.*\[(\d*)/(\w*)/(\d*).*"(GET|POST)\s(https?://)[a-z].*?\.([a-z]+)[^\w.-].*200

我遇到的问题是域名。我得到了 .net、.cgi、.com 和 .htm

我只需要 .net 和 .com,换句话说,在这种情况下出现的第一个域, .net.com

68.134.160.117 - - [09/Mar/2004:22:24:27 -0500] "GET http://www.glocksoft.net/cgi-bin/jenv.cgi HTTP/1.0" 200 1169 "-" "Mozilla/4.0"

220.175.18.42 - - [09/Mar/2004:22:47:30 -0500] "GET http://www.searchlikecrazy.com/cgi-bin/smartsearch.cgi?keywords=Web+Design%20&username=arongyi HTTP/1.0" 200 26166 "http://www.yourwindow.com/searchlikecrazy.htm" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; MyIE2)"

我在哪里得到问题?

谢谢!

4

2 回答 2

1

对于您提供的两个示例,您的正则表达式似乎对我来说都很好(或者我可能只是把问题弄错了)。我使用以下脚本对其进行了测试(对不起,行长):

#!/usr/bin/env python

import re

lines = ['68.134.160.117 - - [09/Mar/2004:22:24:27 -0500] "GET http://www.glocksoft.net/cgi-bin/jenv.cgi HTTP/1.0" 200 1169 "-" "Mozilla/4.0"',
         '220.175.18.42 - - [09/Mar/2004:22:47:30 -0500] "GET http://www.searchlikecrazy.com/cgi-bin/smartsearch.cgi?keywords=Web+Design%20&username=arongyi HTTP/1.0" \
200 26166 "http://www.yourwindow.com/searchlikecrazy.htm" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; MyIE2)"']

regex = re.compile(r'.*\[(\d*)/(\w*)/(\d*).*"(GET|POST)\s(https?://)[a-z].*?\.([a-z]+)[^\w.-].*200')

for line in lines:
    match = regex.match(line)
    if match:
        print match.groups()

输出:

('09', 'Mar', '2004', 'GET', 'http://', 'net')
('09', 'Mar', '2004', 'GET', 'http://', 'com')

Python版本:2.7.1

于 2013-01-29T05:07:10.067 回答
0

我想这可能是您正在搜索的内容。

import re

log_line = '68.134.160.117 - - [09/Mar/2004:22:24:27 -0500] "GET https://www.blog.glocksoft.net/cgi-bin/jenv.cgi HTTP/1.0" 200 1169 "-" "Mozilla/4.0"'

print re.search(r'GET\s\w{3,5}://((\w+\.?)+)', log_line).group(1)
print re.search(r'GET\s\w{3,5}://((\w+\.?)+)', log_line).group(2)

输出:

www.blog.glocksoft.net
net
于 2013-01-29T05:26:27.507 回答