Java .Bak文件解析,使用方法replaceAll()
我想从我的示例文件中省略一些细节。我不希望在写入文件输出后,在解析的文件中出现“[SID:20068]SMB请求BO。此应用程序的通信已被阻止:C:\WINDOWS\system32\ntoskrnl.exe”。由于隐私和安全问题,我省略了文件的其余细节。我尝试使用“\”是因为我认为它与正则表达式函数的特殊字符相链接,但它似乎不起作用,这意味着它仍然没有从输出文件中被忽略 示例文件:Java .Bak文件解析,使用方法replaceAll(),java,regex,Java,Regex,我想从我的示例文件中省略一些细节。我不希望在写入文件输出后,在解析的文件中出现“[SID:20068]SMB请求BO。此应用程序的通信已被阻止:C:\WINDOWS\system32\ntoskrnl.exe”。由于隐私和安全问题,我省略了文件的其余细节。我尝试使用“\”是因为我认为它与正则表达式函数的特殊字符相链接,但它似乎不起作用,这意味着它仍然没有从输出文件中被忽略 示例文件: 20:02:15 SymantecServer CALVIN: teller,[SID: 20068] SMB
20:02:15 SymantecServer CALVIN: teller,[SID: 20068] SMB Request BO detected. Traffic has been blocked from this application: C:\WINDOWS\system32\ntoskrnl.exe
19:58:40 Occurrences: 1,Application: C:/WINDOWS/system32/ntoskrnl.exe,Location: Home - LAN,User: Administrator,Domain: HUMBLE
预期产出:
20:02:15 SymantecServer CALVIN: teller,(....other file details which are omitted due to privacy)
19:58:40 Occurrences: 1,Application: C:/WINDOWS/system32/ntoskrnl.exe,Location: Home - LAN,User: Administrator,Domain: HUMBLE
我的代码:
try {
File file = new File(filename);
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = "", oldtext = "";
while ((line = reader.readLine()) != null) {
oldtext += line + "\r\n";
}
reader.close();
// replace a word in a file
oldtext = oldtext.replaceAll("\\[SID: 20068\\] SMB Request BO detected. Traffic has been blocked from this application: C:\\WINDOWS\\system32\\ntoskrnl.exe,Local: 192.168.28.88,", "");
oldtext = oldtext.replaceAll("\\[SID: 21545\\] SMB Guest Login detected. Traffic has been allowed from this application: C:\\WINDOWS\\system32\\ntoskrnl.exe,", "");
oldtext = oldtext.replaceAll("\\[SID: 23471\\] MS SMB2 Validate Provider Callback RCE detected. Traffic has been blocked from this application: C:\\WINDOWS\\system32\\ntoskrnl.exe,", "");
oldtext = oldtext.replaceAll("\\[SID: 23180\\] MSRPC Server Service Buffer Overflow 2 detected. Traffic has been blocked from this application: C:\\WINDOWS\\system32\\ntoskrnl.exe,", "");
oldtext = oldtext.replaceAll("\"Denial of Service \"\"Ping of Death\"\" attack detected. Description: In a Ping of Death attack, the hacker uses a packet with a size that is larger than the normal standard. When your system encounters a packet of this size, it often crashes, hangs, or reboots.\",", "");
FileWriter writer = new FileWriter("new_bakky.bak");
// the entire file is contained within the String 'oldtext'
// you only need one write operation to output it
writer.write(oldtext);
writer.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
从您的代码来看,您似乎正在替换文字,而实际上并不需要正则表达式(请参见
String\replace
)。请注意,您的代码示例中没有与输入片段匹配的替换查询。是的,我尝试过不使用正则表达式,但它对我也不起作用。仍然与以前的文件相同:(