下面列出的正則表達式將匹配開始EVENT: "[INIT]
和結束[END]";
的任何事件日誌。如果任何感興趣的短語都在事件日誌中,它們將被記錄下來。
請注意使用嵌套捕獲組:(?P<log>...(?P<src_port>...)...)
。外部團隊將捕捉整個模式,包括內部組織捕獲的任何內容。
另請注意,任何不參與比賽的組仍然存在於結果dict
中,其值爲None
。
import re
from pprint import pprint
texts=[
'EVENT: "[INIT]WinEvtLog: Security: AUDIT_SUCCESS(528): Security: Administrator: AMAZON-D071A6F8: AMAZON-D071A6F8: Successful Logon: User Name: Administrator Domain: AMAZON-D071A6F8 Logon ID: (0x0,0x1054A66) Logon Type: 10 Logon Process: User32 Authentication Package: Negotiate Workstation Name: AMAZON-D071A6F8 Logon GUID: - Caller User Name: AMAZON-D071A6F8$ Caller Domain: WORKGROUP Caller Logon ID: (0x0,0x3E7) Caller Process ID: 968 Transited Services: - Source Network Address: 10.0.0.200 Source Port: 60054 [END]";',
'EVENT: "[INIT]Random text with one match Source Port: 60054 And stuff at end [END]";',
'EVENT: "[INIT]Random text with no matches [END]";']
for text in texts:
match = re.match(
r'''
(?x) # Verbose
EVENT:\s"\[INIT] # anchor from beginning
(?P<log> # record entire entry
(?: # consisting of:
(?:Source\sNetwork\sAddress:\s # src_network_address
(?P<src_network_address>\S+))
| # OR
(?:Source\sPort:\s # src_port
(?P<src_port>\S+))
| # OR
.*? # anything else
)* # as many times as required
)
\s\[END]";$ # anchor at end
''',
text)
if(match):
pprint (match.groupdict())
結果:
{'log': 'WinEvtLog: Security: AUDIT_SUCCESS(528): Security: Administrator: AMAZON-D071A6F8: AMAZON-D071A6F8: Successful Logon: User Name: Administrator Domain: AMAZON-D071A6F8 Logon ID: (0x0,0x1054A66) Logon Type: 10 Logon Process: User32 Authentication Package: Negotiate Workstation Name: AMAZON-D071A6F8 Logon GUID: - Caller User Name: AMAZON-D071A6F8$ Caller Domain: WORKGROUP Caller Logon ID: (0x0,0x3E7) Caller Process ID: 968 Transited Services: - Source Network Address: 10.0.0.200 Source Port: 60054',
'src_network_address': '10.0.0.200',
'src_port': '60054'}
{'log': 'Random text with one match Source Port: 60054 And stuff at end',
'src_network_address': None,
'src_port': '60054'}
{'log': 'Random text with no matches',
'src_network_address': None,
'src_port': None}
,只有當要素是有序的和非可選工作。 – 2014-08-27 17:45:51
根據輸入張貼.. – 2014-08-27 17:46:34