Java IO模式识别
我想识别数据部分,我使用如下代码Java IO模式识别,java,regex,Java,Regex,我想识别数据部分,我使用如下代码 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
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(新文件(“C:\\STUDY\\MASTERARBEIT\\DATA\\MHSETS\\HURST\\BIGCONE.1”);
s、 findInLine(“(\\s+\\d+\.\\d{2})+$”;
MatchResult结果=s.match();
对于(int i=1;i如果您知道您总是有8列数据,那么按照以下方式构建您的正则表达式:
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();
StringBuilder sb=新建StringBuilder();
对于(int i=0;i如果您知道您总是有8列数据,那么按照以下方式构建您的正则表达式:
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();
StringBuilder sb=新建StringBuilder();
对于(int i=0;i如果您知道您总是有8列数据,那么按照以下方式构建您的正则表达式:
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();
StringBuilder sb=新建StringBuilder();
对于(int i=0;i如果您知道您总是有8列数据,那么按照以下方式构建您的正则表达式:
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();
StringBuilder sb=新建StringBuilder();
对于(int i=0;i我个人更喜欢使用模式
和匹配器
,如下所示:
StringBuilder sb = new StringBuilder();
for(int i=0;i<8;++i) {
sb.append("\\s+(\\d+\\.\\d+)");
}
s.findInLine(sb.toString());
我个人更喜欢使用模式
和匹配器
,如下所示:
StringBuilder sb = new StringBuilder();
for(int i=0;i<8;++i) {
sb.append("\\s+(\\d+\\.\\d+)");
}
s.findInLine(sb.toString());
我个人更喜欢使用模式
和匹配器
,如下所示:
StringBuilder sb = new StringBuilder();
for(int i=0;i<8;++i) {
sb.append("\\s+(\\d+\\.\\d+)");
}
s.findInLine(sb.toString());
我个人更喜欢使用模式
和匹配器
,如下所示:
StringBuilder sb = new StringBuilder();
for(int i=0;i<8;++i) {
sb.append("\\s+(\\d+\\.\\d+)");
}
s.findInLine(sb.toString());
我使用Scanner.findWithinHorizon设法使其正常工作,但代码非常松散,有时我会收到一个非法状态异常,因为匹配程序一直在运行
但是我用Pattern
和Matcher
来实现它!基本上,您需要抓取输入的每一行,然后使用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 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();
希望有帮助:)
编辑:AliLofti在我之前给出了相同的答案,请相信他:)我使用Scanner.findWithinHorizon设法使其正常工作,但代码非常松散,有时我会遇到非法状态异常,因为matcher一直在运行
但是我用Pattern
和Matcher
来实现它!基本上,您需要抓取输入的每一行,然后使用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 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();
希望有帮助:)
编辑:AliLofti在我之前给出了相同的答案,请相信他:)我使用Scanner.findWithinHorizon设法使其正常工作,但代码非常松散,有时我会遇到非法状态异常,因为matcher一直在运行
但是我用Pattern
和Matcher
来实现它!基本上,您需要抓取输入的每一行,然后使用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 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();
希望有帮助:)
编辑:AliLofti在我之前给出了相同的答案,请相信他:)我使用Scanner.findWithinHorizon设法使其正常工作,但代码非常松散,有时我会遇到非法状态异常,因为matcher一直在运行
但是我用模式
和匹配器
来实现它!基本上,您需要抓取输入的每一行,然后使用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 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();
希望有帮助:)
Edit:AliLofti在我之前给出了相同的答案,请给他评分:)你可以使用matcher.find()
而不是match()
。如果整个字符串与指定的regexp匹配,则进行Match方法测试。如果字符串的一部分与regexp匹配,则find返回true。您可以使用matcher.find()
而不是match()
。如果整个字符串与指定的regexp匹配,则进行Match方法测试。如果字符串的一部分与regexp匹配,则find返回true。您可以使用matcher.find()
而不是match()
。如果整个字符串与指定的regexp匹配,则进行Match方法测试。如果字符串的一部分与regexp匹配,则find返回true。您可以使用matcher.find()
而不是match()
。如果整个字符串与指定的regexp匹配,则进行Match方法测试。如果字符串的一部分与regexp匹配,则find返回true。您需要使用find函数。您需要使用find函数。您需要使用find函数。您需要使用find函数。不,这不是问题所在。Scanner的match()
方法返回最近调用findInLine()
或findWithinHorizon()
所匹配的内容。您正在考虑matches()
方法。不,这不是问题所在。Scanner的match()
方法返回最近调用findInLine()
或findWithinHorizon()
所匹配的内容。您正在考虑matches()
方法。不,这不是问题所在。Scanner的match()
方法返回最近调用findInLine()
或findWithinHorizon()
所匹配的内容。您正在考虑matches()
方法。不,这不是问题所在。Scanner的match()
方法返回最近调用findInLine()
或findWithinHorizon()
所匹配的内容。您正在考虑matches()
方法。您好,非常感谢您的详细建议。现在一切正常。但是你的详细解释对我帮助很大!!!我更了解两者的区别。嗨,谢谢你的详细建议。现在一切正常。但是你的详细解释对我帮助很大!!!我更了解两者的区别。嗨,谢谢你的详细建议。现在一切正常。但是你的详细解释对我帮助很大!!!我更了解两者的区别。嗨,谢谢你的详细建议。现在一切正常。但是你的详细解释对我帮助很大!!!我更了解两者的区别。