Я считаю, что это делает то, что вы ищете:
File file = new File("regextest.txt");
StringBuilder sb = new StringBuilder();
Scanner scanner = new Scanner(file).useDelimiter("\n");
while (scanner.hasNext()) {
String line = scanner.next();
line = line.replaceAll("^Diff\\(", "");
line = line.replaceAll("\\)$", "");
sb.append(line);
}
String combined = sb.toString();
Pattern pattern = Pattern.compile("\\[.+?\\]");
Matcher matcher = pattern.matcher(combined);
while (matcher.find()) {
String extract = combined.substring(matcher.start(), matcher.end());
extract = extract.replaceAll("\\[ ?", "");
extract = extract.replaceAll(" ?\\]", "");
System.out.println(extract);
}
Для вашего regextest.txt
файла вывод выглядит так:
12, C1,C4,C5,C6,C9,C10,C15,C18,C19,C20,C23,C24, C0603, 10nF
10, C2,C3,C7,C8,C13,C16,C17,C21,C22,C27, C0603, 100nF
2, C11,C25, SMT, 1uF LOW ESR 50V
4, C12,C14,C26,C28, C0805, 2u2
4, D1,D2,D4,D9, SOT23, BAS40-04/SOT
4, D3,D5,D6,D7, SMB, SMBJ5.0A
1, D8, SMB, SMBJ15A
2, D10,D11, SMB, SMBJ30A
1, J1, SMT, CON12
2, L1,L2, SMT, 744043471, 470uH
4, L3,L4,L5,L6, 119LNS
...