我正在讀取日誌文件並提取文件中包含的某些數據。我能夠爲日誌文件的每一行提取時間。Java正則表達式提取一個id字符串,基於每個id的循環子字符串
現在我想解壓ID "ieatrcxb4498-1"
。所有的ID都以子字符串ieatrcxb
開頭,我嘗試查詢並返回基於它的完整字符串。
我已經嘗試了其他帖子的許多不同的建議。但我一直不成功,有以下模式:
(?i)\\b("ieatrcxb"(?:.+?)?)\\b
(?i)\\b\\w*"ieatrcxb"\\w*\\b"
^.*ieatrcxb.*$
我也試圖解壓基於完整的ID,在字符串開始i
和1
整理。正如他們所做的一樣。日誌文件
150: 2017-06-14 18:02:21 INFO monitorinfo : Info: Lock VCS on node "ieatrcxb4498-1"
代碼
Scanner s = new Scanner(new FileReader(new File("lock-unlock.txt")));
//Record currentRecord = null;
ArrayList<Record> list = new ArrayList<>();
while (s.hasNextLine()) {
String line = s.nextLine();
Record newRec = new Record();
// newRec.time =
newRec.time = regexChecker("([0-1]?\\d|2[0-3]):([0-5]?\\d):([0-5]?\\d)", line);
newRec.ID = regexChecker("^.*ieatrcxb.*$", line);
list.add(newRec);
}
public static String regexChecker(String regEx, String str2Check) {
Pattern checkRegex = Pattern.compile(regEx);
Matcher regexMatcher = checkRegex.matcher(str2Check);
String regMat = "";
while(regexMatcher.find()){
if(regexMatcher.group().length() !=0)
regMat = regexMatcher.group();
}
//System.out.println("Inside the "+ regexMatcher.group().trim());
}
return regMat;
}
的
行,我需要一個簡單的模式,這將幫我這個忙。
「一個日誌文件並提取文件中包含的某些數據」 - 什麼樣的數據,什麼樣的日誌文件?發表一個例子。 – user3734782
@ user3734782150:2017-06-14 18:02:21 INFO monitorinfo:Info:鎖定節點「ieatrcxb4498-1」上的VCS這是來自日誌文件的數據示例。 –
所以你想提取整行包含ID? – user3734782