【发布时间】:2018-03-17 22:43:42
【问题描述】:
我正在开发一种可以从一些文本文件中提取字段值对的工具。到现在为止,我在windows机器上工作。当我在 linux 上测试该工具时,我的字段数量越来越多。
正则表达式:
([^,;\n\v{}<>\t=:\[\]\"\']+?)[=:][ \t]*(?:\"((?:[^\"\\]|\\.)*)\"|([^\t =:\"\n\v\t\{\}\[\]<>](?!(?!,)\S+[:=])(?:[^\n\v\t\{\}\[\]=:<>](?!(?!,)\S+[:=]))*))
示例文件:
05/02/2011 03:47:12 PM
LogName=Security
SourceName= ##Source_Name##
EventCode=4624
EventType=##Event_Type##
Type=##Type##
ComputerName=##Computer_Name##
TaskCategory=##Task_Category##
OpCode=##OpCode##
RecordNumber=##Record_Number##
Keywords=##Keyword_Success##
Message=An account was successfully logged on.
January 05-11 03:47:12 PM
Subject:
Security ID: ##Domain##\SYSTEM
Account Name: ##Computer_Name##
Account Domain: ##Domain##
Logon ID: 0x##System_Logon_Id##
Jan 27 03:47:12 PM
Logon Information:
Logon Type: ##Logon_Type##
Restricted Admin Mode: ##Restricted_Admin_Mode##
Virtual Account: ##Virtual_Account##
Elevated Token: ##Elevated_Token##
Impersonation Level: ##Impersonation_Level##
New Logon:
Security ID: ##Domain##\##User_Name##
Account Name: ##User_Name##
Account Domain: ##Domain##
Logon ID: 0x##Logon_Id##
Linked Logon ID: ##Linked_Logon_Id##
Network Account Name: ##User_Name2##
Network Account Domain: ##Domain2##
Logon ##GUID##: ##Logon_Guid##
Process Information:
Process ID: 0x##Process_Id##
Process Name: ##Process_Name##
Network Information:
Workstation Name: ##Computer_Name##
Source Network Address: ##Network_Ip##
Source Port: ##Network_Port##
Detailed Authentication Information:
Logon Process: ##Logon_Process##
Authentication Package: ##Authentication_Package##
Transited Services: ##Transited_Services##
Package Name (NTLM only): ##Package_Name##
Key Length: ##Key_Length##
使用 Python 2.7.14 在 Windows 中运行 re.findall() 时的输出:
[('time_field', '05/02/2011 03:47:12 PM'), ('time_field', 'January 05-11 03:47:12 PM'), ('time_field', 'Jan 27 03:47:12 PM'), ('LogName', 'Security'), ('SourceName', '##Source_Name##'), ('EventCode', '4624'), ('EventType', '##Event_Type##'), ('Type', '##Type##'), ('ComputerName', '##Computer_Name##'), ('TaskCategory', '##Task_Category##'), ('OpCode', '##OpCode##'), ('RecordNumber', '##Record_Number##'), ('Keywords', '##Keyword_Success##'), ('Message', 'An account was successfully logged on.'), ('Security ID', '##Domain##\\SYSTEM'), ('Account Name', '##Computer_Name##'), ('Account Domain', '##Domain##'), ('Logon ID', '0x##System_Logon_Id##'), ('Logon Type', '##Logon_Type##'), ('Restricted Admin Mode', '##Restricted_Admin_Mode##'), ('Virtual Account', '##Virtual_Account##'), ('Elevated Token', '##Elevated_Token##'), ('Impersonation Level', '##Impersonation_Level##'), ('Security ID', '##Domain##\\##User_Name##'), ('Account Name', '##User_Name##'), ('Account Domain', '##Domain##'), ('Logon ID', '0x##Logon_Id##'), ('Linked Logon ID', '##Linked_Logon_Id##'), ('Network Account Name', '##User_Name2##'), ('Network Account Domain', '##Domain2##'), ('Logon ##GUID##', '##Logon_Guid##'), ('Process ID', '0x##Process_Id##'), ('Process Name', '##Process_Name##'), ('Workstation Name', '##Computer_Name##'), ('Source Network Address', '##Network_Ip##'), ('Source Port', '##Network_Port##'), ('Logon Process', '##Logon_Process##'), ('Authentication Package', '##Authentication_Package##'), ('Transited Services', '##Transited_Services##'), ('Package Name (NTLM only)', '##Package_Name##'), ('Key Length', '##Key_Length##')]
使用 Python 2.7.6 在 Linux 中运行时的输出:
[('time_field', '05/02/2011 03:47:12 PM'), ('time_field', 'January 05-11 03:47:12 PM'), ('time_field', 'Jan 27 03:47:12 PM'), ('LogName', 'Security'), ('SourceName', '##Source_Name##'), ('EventCode', '4624'), ('EventType', '##Event_Type##'), ('Type', '##Type##'), ('ComputerName', '##Computer_Name##'), ('TaskCategory', '##Task_Category##'), ('OpCode', '##OpCode##'), ('RecordNumber', '##Record_Number##'), ('Keywords', '##Keyword_Success##'), ('Message', 'An account was successfully logged on.'), ('Subject', ''), ('Security ID', '##Domain##\\SYSTEM'), ('Account Name', '##Computer_Name##'), ('Account Domain', '##Domain##'), ('Logon ID', '0x##System_Logon_Id##'), ('Logon Information', ''), ('Logon Type', '##Logon_Type##'), ('Restricted Admin Mode', '##Restricted_Admin_Mode##'), ('Virtual Account', '##Virtual_Account##'), ('Elevated Token', '##Elevated_Token##'), ('Impersonation Level', '##Impersonation_Level##'), ('New Logon', ''), ('Security ID', '##Domain##\\##User_Name##'), ('Account Name', '##User_Name##'), ('Account Domain', '##Domain##'), ('Logon ID', '0x##Logon_Id##'), ('Linked Logon ID', '##Linked_Logon_Id##'), ('Network Account Name', '##User_Name2##'), ('Network Account Domain', '##Domain2##'), ('Logon ##GUID##', '##Logon_Guid##'), ('Process Information', ''), ('Process ID', '0x##Process_Id##'), ('Process Name', '##Process_Name##'), ('Network Information', ''), ('Workstation Name', '##Computer_Name##'), ('Source Network Address', '##Network_Ip##'), ('Source Port', '##Network_Port##'), ('Detailed Authentication Information', ''), ('Logon Process', '##Logon_Process##'), ('Authentication Package', '##Authentication_Package##'), ('Transited Services', '##Transited_Services##'), ('Package Name (NTLM only)', '##Package_Name##'), ('Key Length', '##Key_Length##')]
在 linux 中额外生成而不在 windows 中生成的字段:
('Subject', '')
('Logon Information', '')
('Network Information', '')
('Detailed Authentication Information', '')
在这里,我的困惑是:
- 是否可以在不同的机器上使用相同的正则表达式获得不同的输出?
- 或者问题出在我在两台机器上都使用的 python 版本上。?
- 如果我想同时支持这两种机器,我应该注意什么?
注意: 在这里,我的问题不是关于正则表达式是对还是错。因为调试该正则表达式可能需要更多时间,而且它不是那么优化和整洁。这只是关于这里差异的原因以及我实际上应该记住的内容。
更新:https://regex101.com/r/rDkBxN/1 给出的结果与 windows 相同。
【问题讨论】:
-
看看那个模式,是的,它是关于正则表达式的。
-
这只是我在这里展示的一个示例,这个正则表达式还支持另外 5 种模式,抱歉,我不能这么简单地改变它。
-
Windows 和 Linux 使用不同的约定来分隔文本行:Windows 在换行符之前有一个额外的 Return。这可能与它有关。
-
@ArndtJonasson,谢谢,这似乎是问题所在.. 让我检查一下是否是这种情况。其他建议也会有很大帮助..