我正在寫一個解析器看起來像這樣的文件:用的BufferedReader(Java)的預讀
LOCUS SCU49845 5028 bp DNA PLN 21-JUN-1999
DEFINITION Saccharomyces cerevisiae TCP1-beta gene, partial cds, and Axl2p
(AXL2) and Rev7p (REV7) genes, complete cds.
ACCESSION U49845
VERSION U49845.1 GI:1293613
我想通過某些標記之前的信息(定義,版本等),但一些描述覆蓋多條線路,我確實需要它。使用BufferdReader讀取我的文件時,這是一個問題。 我幾乎通過使用mark()和reset()來計算出它,但是當執行我的程序時,我注意到它只適用於一個標籤,其他標籤以某種方式被跳過。這是我到目前爲止的代碼:
Pattern pTag = Pattern.compile("^[A-Z]{2,}");//regex: 2 or more uppercase letters is a tag
Matcher mTagCurr = pTag.matcher(line);
if (mTagCurr.find()) {
reader.mark(1000);
String nextLine = reader.readLine();
Matcher mTagNext = pTag.matcher(nextLine);
if (mTagNext.find()){
reader.reset();
continue;
}
Pattern pWhite = Pattern.compile("^\\s{6,}");
Matcher mWhite = pWhite.matcher(nextLine);
while (mWhite.find()) {
line = line.concat(nextLine);
}
System.out.println(line);
}
這段代碼應該找到標籤和連接說明,涵蓋多行。我在這裏找到的一些答案建議使用掃描儀。這不適合我。我使用的文件可能非常大(我遇到的最大文件大於50GB),並且使用BufferedReader,我希望在系統上減少壓力。