考虑捕获组中的空白区域

时间:2017-02-15 23:33:07

标签: java regex

我有一个正则表达式,可以从Windows Down-Level格式捕获组安全ID。它剥离域部分只捕获" \"之后的后续ID。

正则表达式:Group:\s+Security\s+ID:\s+.*?\\([^ ]+)

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

但是,如果组名称中有空格,则它不能正确匹配。它仅匹配" VM"

Group:  Security ID:  CORP\VM Admins 

如果有空格,我将如何匹配任一场景?以下是我的用例链接 - https://regex101.com/r/gzFe0J/1

4 个答案:

答案 0 :(得分:2)

在你的情况下,似乎Group Name总是出现在Group之后,如果是这样的话:

使用

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

答案 1 :(得分:2)

一种方法可能是:

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

请参阅your modified example

<小时/> 解释(双反斜杠只需要Java):

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

答案 2 :(得分: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));
}

答案 3 :(得分:0)

您只需要更改
Group:\s+Security\s+ID:\s+.*?\\([^ ]+)
于:
Group:\s+Security\s+ID:\s+.*?\\(\w+ ?\w+)
而已。

(\w+ ?\w+)它与one word匹配,然后匹配一个可选的whitespace,然后匹配您需要的second word

  

您的问题 如果有空格,我将如何匹配任一场景?

匹配作为可选匹配 ?