2017-02-15 26 views
4

我有一個正則表達式,它從Windows Down-Level格式中捕獲組安全ID。它域部分去掉,只經過「\」佔用捕獲組中的空白空間

正則表達式捕獲隨後ID:Group:\s+Security\s+ID:\s+.*?\\([^ ]+)

Group: Security ID: CORP\VirtualUsers (match success) 

但是,如果在該組名稱中的空格不正確地匹配。它只匹配「虛擬機」

Group: Security ID: CORP\VM Admins 

我該如何去匹配兩種情況是否有空間?這裏是我的使用情況的鏈接 - https://regex101.com/r/gzFe0J/1

+0

用['組使匹配作爲可選匹配:\ S +安全\ S + ID:\ S + [^ \\] * \\(。 +?)\ s + Group Name:'](https://regex101.com/r/gzFe0J/2)如果'Group Name:'總是出現在該值之後。 –

+0

查看我的答案,下面有演示和一些解釋。 –

+0

@WiktorStribiżew - 這有效,但我剛剛意識到可能有一個「組名稱:」之後沒有出現的情況。一些WIndows日誌記錄具有以下「帳戶名稱:」。請參閱[鏈接](https://regex101.com/r/yuoAVu/1) – Heisenberg

回答

1

因爲你的ID值總是出現之前Group Name:Account Name:將其添加爲右手上下文和捕獲所有之間Group: Security ID: DOMAIN\Group Name:/Account Name:

Group:\s+Security\s+ID:\s+[^\\]*\\(.+?)\s+(?:Group|Account)\s+Name: 

查看regex demo。該[^\\]*結果會比其他\零個或多個字符一個\之前,\\將匹配\(.+?)將捕捉任何1+字符儘可能少到第一Group Name:Account Name:

如果你的ID只能包含括與非空間的空間,你需要一個簡單的\S+(?: \S+)*模式,無需與先行回火貪婪令牌:

Group:\s+Security\s+ID:\s+[^\\]*\\(\S+(?: \S+)*) 

another regex demo

見在Java demo

String str = "<13>Jan 09 12:33:50 TESTSRV1 AgentDevice=WindowsLog AgentLogFile=Security PluginVersion=7.2.4.86 Source=Microsoft-Windows-Security-Auditing Computer=corp.devnet.com OriginatingComputer=TESTSRV1 User= Domain= EventID=4755 EventIDCode=4755 EventType=8 EventCategory=13826 RecordNumber=1244048130 TimeGenerated=1483983229 TimeWritten=1483983229 Level=0 Keywords=0 Task=0 Opcode=0 Message=A security-enabled universal group was changed. Subject: Security ID: CORP\\TESTUSR1 Account Name: TESTUSR1 Account Domain: CORP Logon ID: 0x220f7a57 Group: Security ID: CORP\\Virtual Users Group Name: VirtualUsers Group Domain: CORP Changed Attributes: SAM Account Name: - SID History: - Additional Information: Privileges: -"; 
Pattern ptrn = Pattern.compile("Group:\\s+Security\\s+ID:\\s+[^\\\\]*\\\\(.+?)\\s+(?:Account|Group)\\s+Name:"); 
Matcher matcher = ptrn.matcher(str); 
while (matcher.find()) { 
    System.out.println(matcher.group(1)); 
} 
+0

這個工作,我認爲它會比使用前瞻性貪婪令牌更少的資源密集型。你會同意嗎?這個線程將給我一個很好的基礎來研究不同的構造技術。我感謝您的幫助。 – Heisenberg

+0

你明白我的意思:[tempered greedy token](http://stackoverflow.com/a/37343088/3832970)是一種相當耗費資源的模式。應該展開以更快地匹配。使用量化組是一種不太「昂貴」的方式來匹配您擁有的字符串。 –

2

在你的情況下,它似乎是集團名稱總是出現後組,如果是這樣的:

使用

Group:\s+Security\s+ID:\s+.*?\\(.*)Group Name 
2

的一種方法是:

Group:.+?\\\\((?:(?![ ]{2,}).)+) 

請參閱your modified example


解釋(雙反斜線只需要 Java):

Group:.+?\\\\   # looks for "Group:", anything lazily afterwards 
        # until a backslash 
((?:(?![ ]{2,}).)+) # neg. lookahead, not two spaces consecutively 
+0

這似乎適用於我的所有3個示例[鏈接](https://regex101.com/r/msHctQ/1) - 進行進一步測試。我真的需要研究我的正則表達式來提高我的技能。非常感謝!!! – Heisenberg

0

你只需要您的從改變:
Group:\s+Security\s+ID:\s+.*?\\([^ ]+)
到:
Group:\s+Security\s+ID:\s+.*?\\(\w+ ?\w+)
就是這樣。


(\w+ ?\w+)它匹配one word然後一個可選whitespace然後second word你需要的。


你的問題我怎麼會去匹配任何一種情況下是否有空間或不?

隨着由?