0

我编写了一个 python 程序来分析服务器日志(文本文件)并找到与用户输入不匹配的字符串。无论如何,程序不考虑十六进制编码的字符串。例如:在以下情况下,程序说没有与用户输入不匹配的值,尽管“www.peoplesmonton.com”可用。请帮我避免这种情况?

for line in lines:
    match = re.search('\\b' + userinput + '\\b',line)

示例文本文件:

https://www.mysite.com/myworks/accaply/inquiry.asp 
http://www.peoplesmonton.com/amb/cgi-bin/bank/bank/ambt%20Bank%20Of%20Frnak%20PLC_asp.htm 
http://www.peoplesmonton.com/comblk/cgi-bin/bank/bank/ambt%20Bank%20Of%20ambt%20PLC_asp.htm 
4

1 回答 1

2

该信息是URL 编码的,因此用于urllib2.unquote解码。

>>> input = '''\
... https://www.mysite.com/myworks/accaply/inquiry.asp 
... http://www.peoplesmonton.com/amb/cgi-bin/bank/bank/ambt%20Bank%20Of%20Frnak%20PLC_asp.htm 
... http://www.peoplesmonton.com/comblk/cgi-bin/bank/bank/ambt%20Bank%20Of%20ambt%20PLC_asp.htm 
... '''
>>> import urllib2
>>> print urllib2.unquote(input)
https://www.mysite.com/myworks/accaply/inquiry.asp 
http://www.peoplesmonton.com/amb/cgi-bin/bank/bank/ambt Bank Of Frnak PLC_asp.htm 
http://www.peoplesmonton.com/comblk/cgi-bin/bank/bank/ambt Bank Of ambt PLC_asp.htm 
于 2013-04-01T11:13:45.457 回答