2

I have the following gps position String

#114.034407,E,22.648272,N,0.00,0.00#010104#004500#17.034407,E,22.648272,N,0.00,0.00#010104#004500#5.034407,E,22.648272,N,0.00,0.00#010104#004500

and I want to part it the longitude position (#17.,#5.). All longitudes position starts with # and contains . point after the fitst or first and second or first, second and third. How can I get this result with regular expression?

Result:

#114.034407,E,22.648272,N,0.00,0.00#010104#004500
#17.034407,E,22.648272,N,0.00,0.00#010104#004500
#5.034407,E,22.648272,N,0.00,0.00#010104#004500

Code

  Pattern pattern = Pattern.compile("regular expression");
    Matcher matcher = pattern.matcher(gpsPacket);
    matcher.matches();
water
  • 405
  • 1
  • 5
  • 13

2 Answers2

1

You can use this regex:

(?=#\d{1,3}\.)

with this code:

import java.util.*;
import java.lang.*;
import java.io.*;

class YourClass
{
    public static void main (String[] args) throws java.lang.Exception
    {
        String str = "#114.034407,E,22.648272,N,0.00,0.00#010104#004500#17.034407,E,22.648272,N,0.00,0.00#010104#004500#5.034407,E,22.648272,N,0.00,0.00#010104#004500";
        String delimiters = "(?=#\\d{1,3}\\.)";

        String[] coordinates = str.split(delimiters);
        for(String coordinate : coordinates) {
            System.out.println(coordinate);
        } 
    }
}

It will split the string on a # followed by 1 to 3 number then a dot. Live demo

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
  • Thanks for your answer. It works fine with the example in the question but if I have the following String "#114.034407,E,22.648272,N,0.00,0.00#010104#004500#114.034407,E,22.648272,N,0.00,0.00#010104#004500" I am getting Array with 3 Strings where the first String is empty (without value). Is it possible to fix the regular expression to adjust this case too? – water Sep 30 '16 at 11:50
  • @water I can't reproduce your issue. Maybe you should try on Ideone in order to share or manually remove empty values of the array. – Thomas Ayoub Sep 30 '16 at 11:59
0

This is the regex based upon the information given.

It pulls the three data sets out of your input.

If there are more rules than what you have minimally specified you will have to make adjustments.

I suggest going to a website and practicing. There is a lot of helpful information here RegExr, along with the ability to practice live.

(#[\d]{1,3}.[\d]{6},E,[\d]{2}.[\d]{6},N,[\d]{1}.[\d]{2},[\d]{1}.[\d]{2}#[\d]{6}#[\d]{6})
JynXXedRabbitFoot
  • 996
  • 1
  • 7
  • 17