【问题标题】:Reading text between quotation marks读取引号之间的文本
【发布时间】:2018-01-11 15:26:24
【问题描述】:

这是我正在尝试处理的一段文字:

lat="52.336575" lon="6.381008">< time>2016-12-19T12:12:27Z< /time>< name>Foto 8 </name>< desc>Dag 4 E&amp;F 
Geb 1.4 
Hakhoutstoof < /desc>< /wpt>

我正在尝试提取“”之间的坐标并将“”之间的值放入一个字符串中,但我无法让它工作......

这是我的代码(到目前为止):

public void openFile() {
    Chooser = new JFileChooser("C:\\Users\\danie\\Desktop\\");
    Chooser.setAcceptAllFileFilterUsed(false);
    Chooser.setDialogTitle("Open file");
    Chooser.addChoosableFileFilter(new FileNameExtensionFilter("*.gpx", 
    "gpx"));
    int returnVal = Chooser.showOpenDialog(null);

    try {
        Dummy = new Scanner(Chooser.getSelectedFile());
    } catch (FileNotFoundException E) {
        System.out.println("Error: " + E);
    }
}

public void createDummy() {
    Dummy.useDelimiter("<wpt");
    if (Dummy.hasNext()) {
        String Meta = Dummy.next();
    }
    Dummy.useDelimiter("\\s[<wpt]\\s|\\s[</wpt>]\\s");
    try {
        while (Dummy.hasNext()) {
            String Test = Dummy.next();
            DummyFile = new File("Dummy.txt");
            Output = new PrintWriter(DummyFile);
            Output.print(Test);
            Output.println();
            Output.flush();
            Output.close();          
        }

        Reader = new FileReader(DummyFile);
        Buffer = new BufferedReader(Reader);
        TestFile = new File("C:\\Users\\danie\\Desktop\\Test.txt");
        Writer = new PrintWriter(TestFile);
        String Final;
        while ((Final = Buffer.readLine()) != null) {
            String WPTS[] = Final.split("<wpt");
            for (String STD:WPTS) {
                Writer.println(STD);
                Writer.flush();
                Writer.close();
            }               
        }

    } catch (IOException EXE) {
        System.out.println("Error: " + EXE);
    }
    Dummy.close();
    }
}

我真的是 Java 新手 :(

【问题讨论】:

  • 看到这个问题,它应该会为你指明正确的方向:stackoverflow.com/questions/1473155/…
  • 阅读有关带有组的正则表达式,这应该以一种简洁的方式解决问题。并使用Regex101 测试您的模式。
  • 该文本看起来非常接近 XML。如果是,则应使用 XML 解析器进行解析,而不是使用正则表达式。
  • 您可以使用引号字符拆分字符串作为您拆分的内容,然后在它提供给您的数组的位置 1 和 3 处获取字符串。

标签: java quotation-marks


【解决方案1】:

我认为下面的代码可以解决问题... “字符串”仅用于测试正则表达式

    final String string = "lat=\"52.336575\" lon=\"6.381008\">< time>2016-12-19T12:12:27Z< /time>< name>Foto 8 </name>< desc>Dag 4 E&amp;F \nGeb 1.4 \n" + "Hakhoutstoof < /desc>< /wpt>";

    final String latitudeRegex = "(?<=lat=\")[0-9]+\\.[0-9]*";
    final Pattern latitudePattern = Pattern.compile(latitudeRegex);
    final Matcher latitudeMatcher = latitudePattern.matcher(string);

    //finds the next (in this case first) subsequence matching the given regex
    latitudeMatcher.find();
    String latitudeString = latitudeMatcher.group();
    double lat = Double.parseDouble(latitudeString); //group returns the match matched by previous match
    System.out.println("lat: " + lat);

要获取经度,只需在正则表达式中将 lat 替换为 lon

这个站点对于创建正则表达式非常有用 https://regex101.com/ 你甚至可以在这个站点创建 java 代码

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2013-11-16
    • 1970-01-01
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多