在同一个String中匹配许多结果

时间:2013-05-20 10:06:26

标签: python regex python-2.7

我正在尝试从日志文件中获取其行格式不同的数据,但保证将重要信息放在[]中,例如:

[User] has [do something] on [system] at [time]

[system] encounters [exception] at [time]

如果可能的话,我想写一个正则表达式来获取每个日志行中的所有信息,即正则表达式必须匹配同一行中的许多resutl。例如:

  • [Admin] has [logged out] on [admin page] at [Monday 20 May, 11:00] 将返回Admin, logged out, admin page, Monday 20 May, 11:00
  • [Order page] encounters [NullPointerException] at [Monday 20 May,
    11:00]
    将返回OrderPage, NullPointerException, Monday 20 May, 11:00

我正在研究python,但其他语言或纯正则表达式的答案都很好。感谢

2 个答案:

答案 0 :(得分:3)

>>> import re
>>> text = "[Admin] has [logged out] on [admin page] at [Monday 20 May, 11:00]"
>>> re.findall(r'\[([^\]]*)\]', text)
['Admin', 'logged out', 'admin page', 'Monday 20 May, 11:00']

详细:

>>> text = "[Order page] encounters [NullPointerException] at [Monday 20 May, 11:00]"
>>> re.findall(r'''\[            # a literal [ character (needs backslash escape)
                     (           # save following group
                       [^\]]     # match any character except literal ]
                       *         # match as many as possible of these
                     )           # end group
                    \]           # a literal ] character
''', text, flags=re.VERBOSE)
['Order page', 'NullPointerException', 'Monday 20 May, 11:00']

答案 1 :(得分:2)

或者作为紧凑的perl单线。与 jamylak 使用相同的正则表达式:

perl -pne '$_=join(", ",/\[([^\]]*)\]/g)."\n"'