Java IO模式识别
Java IO Pattern recognition
BIG CONE SPRUCE,SOUTHERN CALIFORNIA, U.S.A., 1458-1966, 509 VALUES
120.00 64.00 56.00 88.00 109.00 100.00 18.00 58.00
97.00 82.00 57.00 116.00 114.00 102.00 78.00 105.00
95.00 76.00 89.00 147.00 114.00 92.00 96.00 95.00
我想识别数据部分,我使用代码如下
Scanner s = new Scanner(new File("C:\STUDY\MASTERARBEIT\DATA\MHSETS\HURST\BIGCONE.1"));
s.findInLine("(\s+\d+\.\d{2})+$");
MatchResult result = s.match();
for (int i=1; i<=result.groupCount(); i++)
System.out.println(result.group(i));
s.close();
但是识别失败了,请大家帮我指出是哪一部分不对。我试过很多
时间修改了,还是不行。非常感谢!
如果您知道您总是有 8 列数据,那么可以这样构建您的正则表达式:
StringBuilder sb = new StringBuilder();
for(int i=0;i<8;++i) {
sb.append("\s+(\d+\.\d+)");
}
s.findInLine(sb.toString());
此外,我只是通过删除您输入的第一行 (BIG CONE SPRUCE,...
) 才使它起作用。
我个人更喜欢使用 Pattern
和 Matcher
,像这样:
try {
Scanner s = new Scanner(new File("C:\STUDY\MASTERARBEIT\DATA\MHSETS\HURST\BIGCONE.1"));
Pattern pattern = Pattern.compile("(\s+\d+\.\d{2})+$");
String data;
while (s.hasNextLine()) {
data = s.nextLine();
Matcher matcher = pattern.matcher(data);
if (matcher.find()) {
System.out.println(matcher.group(0)); // Or whatever you want
}
}
s.close();
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
我有点设法使用 Scanner.findWithinHorizon 让它工作,但代码非常草率,有时我得到一个 IllegalStateException 因为匹配器一直在运行...
但我使用 Pattern
和 Matcher
让它工作了!基本上你只是抓取输入的每一行,然后使用 Pattern.matcher()
的结果如下:
Scanner s = new Scanner(new File("BIGCONE.1"));
Pattern p = Pattern.compile("(\d+\.\d{2})+");
while (s.hasNextLine()) {
String line = s.nextLine();
Matcher m = p.matcher(line);
System.out.print("line:");
while(m.find()) {
System.out.print(" "+m.group());
}
System.out.println();
}
s.close();
它给了我这个输出:
line:
line: 120.00 64.00 56.00 88.00 109.00 100.00 18.00 58.00
line: 97.00 82.00 57.00 116.00 114.00 102.00 78.00 105.00
line: 95.00 76.00 89.00 147.00 114.00 92.00 96.00 95.00
希望对您有所帮助:)
编辑: AliLofti 在我之前给出了相同的答案,感谢他:)
您可以使用 matcher.find()
而不是 match()
。 Match 方法测试整个字符串是否与指定的正则表达式匹配。如果字符串的一部分与正则表达式匹配,则找到 return true。
BIG CONE SPRUCE,SOUTHERN CALIFORNIA, U.S.A., 1458-1966, 509 VALUES
120.00 64.00 56.00 88.00 109.00 100.00 18.00 58.00
97.00 82.00 57.00 116.00 114.00 102.00 78.00 105.00
95.00 76.00 89.00 147.00 114.00 92.00 96.00 95.00
我想识别数据部分,我使用代码如下
Scanner s = new Scanner(new File("C:\STUDY\MASTERARBEIT\DATA\MHSETS\HURST\BIGCONE.1"));
s.findInLine("(\s+\d+\.\d{2})+$");
MatchResult result = s.match();
for (int i=1; i<=result.groupCount(); i++)
System.out.println(result.group(i));
s.close();
但是识别失败了,请大家帮我指出是哪一部分不对。我试过很多 时间修改了,还是不行。非常感谢!
如果您知道您总是有 8 列数据,那么可以这样构建您的正则表达式:
StringBuilder sb = new StringBuilder();
for(int i=0;i<8;++i) {
sb.append("\s+(\d+\.\d+)");
}
s.findInLine(sb.toString());
此外,我只是通过删除您输入的第一行 (BIG CONE SPRUCE,...
) 才使它起作用。
我个人更喜欢使用 Pattern
和 Matcher
,像这样:
try {
Scanner s = new Scanner(new File("C:\STUDY\MASTERARBEIT\DATA\MHSETS\HURST\BIGCONE.1"));
Pattern pattern = Pattern.compile("(\s+\d+\.\d{2})+$");
String data;
while (s.hasNextLine()) {
data = s.nextLine();
Matcher matcher = pattern.matcher(data);
if (matcher.find()) {
System.out.println(matcher.group(0)); // Or whatever you want
}
}
s.close();
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
我有点设法使用 Scanner.findWithinHorizon 让它工作,但代码非常草率,有时我得到一个 IllegalStateException 因为匹配器一直在运行...
但我使用 Pattern
和 Matcher
让它工作了!基本上你只是抓取输入的每一行,然后使用 Pattern.matcher()
的结果如下:
Scanner s = new Scanner(new File("BIGCONE.1"));
Pattern p = Pattern.compile("(\d+\.\d{2})+");
while (s.hasNextLine()) {
String line = s.nextLine();
Matcher m = p.matcher(line);
System.out.print("line:");
while(m.find()) {
System.out.print(" "+m.group());
}
System.out.println();
}
s.close();
它给了我这个输出:
line:
line: 120.00 64.00 56.00 88.00 109.00 100.00 18.00 58.00
line: 97.00 82.00 57.00 116.00 114.00 102.00 78.00 105.00
line: 95.00 76.00 89.00 147.00 114.00 92.00 96.00 95.00
希望对您有所帮助:)
编辑: AliLofti 在我之前给出了相同的答案,感谢他:)
您可以使用 matcher.find()
而不是 match()
。 Match 方法测试整个字符串是否与指定的正则表达式匹配。如果字符串的一部分与正则表达式匹配,则找到 return true。