Python正则表达式:在一行而不是列表中返回匹配?

时间:2016-04-11 01:32:45

标签: python regex parsing

我正在使用正则表达式来解析日志文件以提取不同的字段以基本上创建数据集。正则表达式工作正常:

import re

example_line = '65.55.106.233 - - [03/Sep/2009:16:31:55 -0800] "GET /gallery/main.php?g2_view=core.DownloadItem&g2_itemId=27492&g2_serialNumber=2 HTTP/1.1" 200 4457 "-" "Mozilla/4.0"'



print re.match(regex, example_line).groups()

输出:

('65.55.106.233', '-', '-', '03/Sep/2009:16:31:55 -0800', 'GET /gallery/main.php?g2_view=core.DownloadItem&g2_itemId=27492&g2_serialNumber=2 HTTP/1.1', '200', '4457', '-', 'Mozilla/4.0')

但是我想知道是否有办法将匹配分组到由单个空格分隔的单行而不是将匹配分组到列表中。

因此,我想打印出来,而不是上面显示的输出:

65.55.106.233 - - 03/Sep/2009:16:31:55 -0800 GET /gallery/main.php?g2_view=core.DownloadItem&g2_itemId=27492&g2_serialNumber=2 HTTP/1.1 200 4457 - Mozilla/4.0

1 个答案:

答案 0 :(得分:2)

print ' '.join(re.match(regex, example_line).groups())
相关问题