我想在文本文件中找到“$$$$”模式的实例数。以下方法适用于某些文件,但不适用于所有文件。例如,它不适用于以下文件(http://www.hmdb.ca/downloads/structures.zip - 它是一个带有.sdf扩展名的压缩文本文件)我找不到原因?我也试图逃避空格。没有运气。当有超过35000个“$$$$”模式时,它返回11。请注意,速度至关重要。因此,我不能使用任何较慢的方法。FindWithinHorizon无法匹配
public static void countMoleculesInSDF(String fileName)
{
int tot = 0;
Scanner scan = null;
Pattern pat = Pattern.compile("\\$\\$\\$\\$");
try {
File file = new File(fileName);
scan = new Scanner(file);
long start = System.nanoTime();
while (scan.findWithinHorizon(pat, 0) != null) {
tot++;
}
long dur = (System.nanoTime() - start)/1000000;
System.out.println("Results found: " + tot + " in " + dur + " msecs");
} catch (Exception e) {
e.printStackTrace();
} finally {
scan.close();
}
}
非常感谢您的努力。优秀的答案。 – lochi
@lochi不客气! – A4L