【发布时间】:2014-12-23 21:28:54
【问题描述】:
我编写了一个用于解析模板的扫描仪实用程序。
考虑代码 sn-p :
String input = FileUtils.readFileToString(new File("input file path"));
Scanner scanner = new Scanner(input);
scanner.useDelimiter(System.getProperty("line.separator"));
System.out.println("Checking");
while(scanner.hasNext())
{
System.out.print(scanner.hasNext("\\s*#[^\\n]*"));
System.out.println(" : " + scanner.nextLine());
}
输入文件内容:
# Line 1
#######################
# Check
# Matched with spaces
#
// End of file
注意:输入中不存在文件行的结尾。
产生的输出:
Checking
true : # Line 1
true : #######################
true :
true : # Check
true : # Matched with spaces
true : #
false :
false :
我的问题是,为什么第三行 hasNext() 不以 '#' 开头也会返回 true ?
任何帮助将不胜感激。
【问题讨论】:
标签: java regex java.util.scanner