2016-06-20 43 views
0

我目前正在從一個.audit文件填充一個List,並將兩條信息提取到一個Map中。下面是該文件的樣子:如何同時使用2個正則表達式從文件填充地圖?

type   : REGISTRY_SETTING 
    description : "1.9.56 Network security: Do not store LAN Manager hash value on next password change: Enabled" 
    info   : "This control defines whether the LAN Manager (LM) hash value for the new password is stored when the password is changed." 
    solution  : "Make sure 'Do not store LAN Manager hash value on next password change' is Enabled." 
    reference : "PCI-DSS|8.4,800-53|AC-3,800-53|SC-5,800-53|CM-7,800-53|CM-6,CCE|CCE-8937-5" 
    see_also  : "https://benchmarks.cisecurity.org/tools2/windows/CIS_Microsoft_Windows_7_Benchmark_v1.2.0.pdf" 
    value_type : POLICY_DWORD 
    reg_key  : "HKLM\System\CurrentControlSet\Control\Lsa" 
    reg_item  : "NoLMHash" 
    value_data : 1 



    type   : REGISTRY_SETTING 
    description : "1.13.3 Notify antivirus programs when opening attachments: Enabled" 
    info   : "This control defines whether antivirus program to be notified when opening attachments." 
    solution  : "Make sure 'Notify antivirus programs when opening attachments' is Enabled." 
    reference : "800-53|SI-3,PCI-DSS|5.1.1,CCE|CCE-10076-8,PCI-DSS|5.1" 
    see_also  : "https://benchmarks.cisecurity.org/tools2/windows/CIS_Microsoft_Windows_7_Benchmark_v1.2.0.pdf" 
    value_type : POLICY_DWORD 
    reg_key  : "HKU\Software\Microsoft\Windows\CurrentVersion\Policies\Attachments" 
    reg_item  : "ScanWithAntiVirus" 
    value_data : 3 
    reg_ignore_hku_users : "S-1-5-18,S-1-5-19,S-1-5-20" 

我需要的地圖是在一個<description,value_data>格式,不管什麼來不來後value_data如:

Key: "1.9.56 Network security: Do not store LAN Manager hash value on next password changed." 

Value: 1 

這是我目前用於填充碼地圖與它的鍵值:

String descriptionString = Pattern.quote("description") + "(.*?)" + Pattern.quote("info"); 


Pattern descriptionPattern = Pattern.compile(descriptionString); 


Matcher descriptionMatcher = descriptionPattern.matcher(auditContentList.get(i)); 


while(descriptionMatcher.find()) 
{ 
     System.out.println("Key found "); 
     customItemMap.put(descriptionMatcher.group(1),""); 
} 

問題是我不能同時使用兩個正則表達式在任何給定的時間來填充地圖相同的索引。有沒有更好的方法來做到這一點?

+4

爲什麼不逐行讀取文件中的行?每一行都有模式「name:value」,所以它會容易得多。 – waltersu

+0

我同意。對於此問題,正則表達式都不足並且過度工程 –

回答

0

您的樣本數據,也許你應該試試這個:

description.*?"(.*?)"[\s\S]*?value_data.*?(\d+) 

我已經測試 [here]

+0

不幸的是,當樣本數據如下時,組(2)未正確填充: –

+0

類型\t \t:REGISTRY_SETTING 說明:「1.13.9屏幕保護程序超時=至多900秒「 info \t \t:」此控件定義屏幕保護程序的超時設置。「 解決方案\t:「確保'屏幕保護程序超時'設置爲最大900秒。」 「CCE | CCE-10148-5,PCI-DSS | 2.2.3,800-53 | AC-1」 參考\t:「https://benchmarks.cisecurity.org/tools2/windows/CIS_Microsoft_Windows_7_Benchmark_v1.2.0」。 pdf「 value_type \t:POLICY_DWORD value_data \t:[MIN..900] –

相關問題