1

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

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

或者

[system] encounters [exception] at [time]

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

  • [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,但其他语言或纯正则表达式的答案很好。谢谢

4

2 回答 2

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']
于 2013-05-20T10:08:37.880 回答
2

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

perl -pne '$_=join(", ",/\[([^\]]*)\]/g)."\n"'
于 2013-05-20T10:23:27.453 回答