我的方法需要一个文件,并试图提取报头###Title###
和关闭###---###
之间的文本。我需要它提取多行并将每行放入数组中。但由于readAllLines()
将所有行转换为数组,我不知道如何比较和匹配它。Java匹配器:如何匹配多个行与一个正则表达式
public static ArrayList<String> getData(File f, String title) throws IOException {
ArrayList<String> input = (ArrayList<String>) Files.readAllLines(f.toPath(), StandardCharsets.US_ASCII);
ArrayList<String> output = new ArrayList<String>();
//String? readLines = somehow make it possible to match
System.out.println("Checking entry.");
Pattern p = Pattern.compile("###" + title + "###(.*)###---###", Pattern.DOTALL);
Matcher m = p.matcher(readLines);
if (m.matches()) {
m.matches();
String matched = m.group(1);
System.out.println("Contents: " + matched);
String[] array = matched.split("\n");
ArrayList<String> array2 = new ArrayList<String>();
for (String j:array) {
array2.add(j);
}
output = array2;
} else {
System.out.println("No matches.");
}
return output;
}
这是我的文件,我100%确定编译器正在读取正确的文件。
###Test File###
Entry 1
Entry 2
Data 1
Data 2
Test 1
Test 2
###---###
输出说“没有匹配”。而不是条目。
你的问题到底是什么? –
所有这些都是在一行还是在图片中? – Keerthivasan
@ PM77-1我如何让它匹配而不是返回“不匹配”。方法? –