-1

以下のような文字列があるとします

String s1 = "This is a new direction. Address located. \n\n\n 0.35 miles from location";

今、「場所から 0.35 マイル」のみを抽出したいと考えています。この数値を他の数値と比較するには、「0.35」にもっと興味があります。

文字列 s1 は、次のパターンの場合もあります。

String s1 = "This is not a new direction. Address is not located. \n\n\n 10.25 miles from location";

また

String s1 = "This is not a new direction. Address is located. \n\n\n 11.3 miles from location";

Plsは私が結果を達成するのを助けます. ありがとう!

私はこれを試しました

String wholeText = texts.get(i).getText();
if(wholeText.length() > 1) {
    Pattern pattern = Pattern.compile("[0-9].[0-9][0-9] miles from location");
    Matcher matcg = pattern.matcher(wholeText);
    if (match.find()) {
        System.out.println(match.group(1));
    }

でも、xx.xxマイルだとどうしたらいいのかわからない…

4

1 に答える 1

2

これは、...ab.cd... の形式の任意の数値で機能するはずです。

public static void main(String[] args){
    String s  = "This is a new direction. Address located. " +
            "\n\n\n 0.35 miles from location";
    Pattern p = Pattern.compile("(\\d+\\.\\d+)");
    Matcher m = p.matcher(s);
    while (m.find()) {
      System.out.println(m.group());
    }
}
于 2013-04-19T16:45:22.150 に答える