【发布时间】:2014-12-15 19:20:41
【问题描述】:
我有一个文本文件,其中包含我需要解析的一些文章。
我需要检索每篇文章中的每个单词,不包括句号、逗号等。文章由特定的两行分隔,我正在尝试使用正则表达式模式来查找这些点。
文档示例如下:
.I 1
.W
this is article one.
.I 2
.W
this is article two.
.I 3
.W
this is article three.
下面的代码似乎找到了第一次出现的.I 1 并添加了所有后续单词,但是一旦到达下一个分隔符,它会将其添加为单词而不是跳过它。
Scanner scanner = new Scanner(document);
scanner.useDelimiter("[^\\w']+");
String separator;
while (scanner.hasNext()){
separator = scanner.findInLine(Pattern.compile(".I \\d"));
if (separator!= null) {
System.out.println("Found: " + separator);
scanner.nextLine();
scanner.nextLine();
}
list.add(scanner.next());
}
scanner.close();
如果可能的话,我还希望能够获取实际的文章编号,即附加到每个分隔符上的编号。
我的代码有什么问题?
【问题讨论】:
标签: java regex parsing java.util.scanner