-1

Say I have a String like below

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

now I want to extract "0.35 miles from location" only. I'm more interested in "0.35" to compare this number with something else.

The String s1 may be of following pattern as well.

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

or

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

Pls help me to achieve the result. Thanks!

I tried this

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));
    }

But I don't know what to do when it's xx.xx miles...

Damien-Amen
  • 7,232
  • 12
  • 46
  • 75

1 Answers1

2

This should work for any number formatted as ...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());
    }
}
AllTooSir
  • 48,828
  • 16
  • 130
  • 164