Question

How do I find multiple float numbers in a string?

This is the code that I used from SO but can only get one float.

public void getPattern(View v){
    String s = "http://maps.google.com/maps?f=q&hl=en&q=04.445560,100.424200&ie=UTF8&z=16&iwIoc=addr&om=1speed:000.0&imei=1234567564";
    Pattern p = Pattern.compile("\\d+\\.\\d+");
    Matcher m = p.matcher(s);
    if (m.find()) {
        test.setText(m.group(0));
    } else{
        test.setText("No float");
    }
}

I need to get another float from the string but unable to understand the regex pattern.compile options. This code only get the first float which is 04.445560. The other latlong data i was unable to get.

Was it helpful?

Solution

Your regular expression is correct here.

An if statement executes a certain section of code only if a particular condition is true. The while statement continually executes a block of statements while a particular condition is true.

Example:

String s  = "http://maps.google.com/maps?f=q&hl=en&q=04.445560,100.424200&ie=UTF8&z=16&iwIoc=addr&om=1speed:000.0&imei=1234567564";
Pattern p = Pattern.compile("\\d+\\.\\d+");
Matcher m = p.matcher(s);
while (m.find()) {
   System.out.println(m.group());
}

Output

04.445560
100.424200
000.0

OTHER TIPS

You will need to change your if to a while

To get it to do what you want I imagine that setting test.setText will need to be modified to append the String.

test.setText(test.getText() + m.group(0));

or something similar by using a StringBuffer.

I think that there is just one small mistake in your code, you just have to change your if to while and you will get all the occurences of float values in the string provided.

public void getPattern(View v){
    String s = "http://maps.google.com/maps?f=q&hl=en&q=04.445560,100.424200&ie=UTF8&z=16&iwIoc=addr&om=1speed:000.0&imei=1234567564";
    Pattern p = Pattern.compile("\\d+\\.\\d+");
    Matcher m = p.matcher(s);
    while (m.find()) {
        test.setText(m.group());
    } else{
        test.setText("No float");
    }
}

Also, if you want to know the start and end indexes you may use m.start() and m.end() functions to get it.

I haven't use Pattern or Matcher before, but looking the code, you should do a loop to get all the floats with m.group(i).

Something like:

int i = 0;
while(m.find()){
    m.group(i);
    i++;
}

EDIT:

After having looked the Matcher javadoc, you can use groupCount to get the times your expression is found.

int length = m.groupCount();
String temp="";
for (int i = 0; i < length; i++) {
    Log.i("Float found:" + (i+1), m.group(i));
    temp+= "  " + m.group(i)
}
if (0 < length) {
    test.setText(temp);
} else{
    test.setText("No float");
}

Thanks all.. based on the solutions provided I solved my problem with the following code.

        //String s = "http://maps.google.com/maps?f=q&hl=en&q=05.445560,100.424200&ie=UTF8&z=16&iwIoc=addr&om=1speed:000.0&imei=3556890194";
 Pattern p = Pattern.compile("\\d+\\.\\d+");
    Matcher m = p.matcher(s);
    int i =0;
    Float n[] = new Float[4];
        while (m.find()) {
               System.out.println(m.group());
               n[i]= Float.parseFloat(m.group());
               i++;
            }   
float lat = n[0];
float long = n[1];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top