将文本文件解析为数字输出

时间:2015-10-27 00:17:00

标签: java regex parsing lex

我想逐行读取文件并解析它。

我的目标是找到像这样的行:

V X = 51:52.77 N 001:13.33 W

数字可能不同

这是我现在的代码:

package kml_gen;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Kml_gen {

private final Path fFilePath;

public static void main(String... aArgs) throws IOException {
    Kml_gen parser = new Kml_gen("C:\\Users\\DSR\\Documents\\master\\kml_gen\\uk_air_2002.txt");
    parser.processLineByLine();
    log("Done.");
}

/**
 * Constructor.
 *
 * @param aFileName full name of an existing, readable file.
 */
public Kml_gen(String aFileName) {
    fFilePath = Paths.get(aFileName);
}

/**
 * Template method that calls {@link #processLine(String)}.
 *
 * @throws java.io.IOException
 */
public final void processLineByLine() throws IOException {
    try (Scanner scanner = new Scanner(fFilePath)) {
        while (scanner.hasNextLine()) {
            processLine(scanner.nextLine());
        }
    }
}

/*
 * @param aLine
 */
protected void processLine(String aLine) {
    //use a second Scanner to parse the content of each line
    //regex [V]\\s*[X][=]\\d+\\:\\d+\\.\\d+\\s*[N]\\s*\\d+\\:\\d+\\.\\d+\\s*[W]   
    String patternstr = "[V]\\s[X][=]\\d*\\:\\d*\\.\\d*\\s[N]\\s\\d*\\:\\d*\\.\\d*\\s[W]";
    Pattern pattern = Pattern.compile(patternstr);
    Matcher matcher = pattern.matcher(aLine);
    boolean matches = matcher.matches();
    if (matcher.find()) {
        /* example of text to be parsed: 
         V X=51:52.77 N 001:13.33 W */
        if (matches) {
            System.out.println("V X=" + matcher.group() + ":" + matcher.group() + "." + matcher.group() + " N "
                    + matcher.group() + ":" + matcher.group() + "." + matcher.group() + " W");
        }
    } else {
        log("Empty or invalid line. Unable to process.");
    }
}

private static void log(Object aObject) {
    System.out.println(String.valueOf(aObject));
}

由于我的输出始终为“空行或无效行。无法处理。”。我真的会帮助你,谢谢你。

编辑:这里有一些文字可供使用,我只考虑以“V X = ...”开头的行。

AC Q

AN PARA Weston

AL SFC

AH FL120

V X = 51:52.77 N 001:13.33 W

DC 1.5

1 个答案:

答案 0 :(得分:3)

//.matches() and .find() can't be used twice,should remove .matches()
String patternstr = "V\\sX=(\\d+):(\\d+)\\.(\\d+)\\sN\\s(\\d+):(\\d+)\\.(\\d+)\\sW";
//matcher.group();  match all the line
System.out.println("V X=" + matcher.group(1) + ":" + matcher.group(2) 
                    + "." + matcher.group(3) + " N " + matcher.group(4) + ":" 
                          + matcher.group(5) + "." + matcher.group(6) + " W");