0

Hi I have a Java program which runs and finds my machines longitude and latitude coordinates, it outputs this as a long string show below,

htt://maps.google.com/maps?q=52.258301,+-7.111900+(192.168.159.1Country:Ireland,City:Waterford-by htt://www.javaquery.com)&iwloc=A&hl=en

What I am trying to do now is to only extract from this string: the IP address and the two coordinates, I have been successful in acquiring the IP address but cant seem to get the two coordinates. The end result would hopefully be

192.168.159.1,52.258301,+-7.111900

So far ive used these expressions to get the IP address

(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)

Which works just fine and then attempted to get the coordinates using this

[0-9]+(\\.[0-9][0-9]?)?

but it only gets the first coordinate followed by it again

Thanks

3
  • 4
    htt instead of http, is that a typo? Commented Mar 5, 2013 at 14:46
  • What regex have you tried out ? Commented Mar 5, 2013 at 14:48
  • Editted to show what ive tried, and yes sorry its just a typo it really is http Commented Mar 5, 2013 at 14:51

2 Answers 2

1

try with this regex:

"(?<=\\?q=)([^(]*)\\(([\\d.]*)"

group(1) is 52.258301,+-7.111900+

group(2) is the ip

EDIT add codes for the regex matching/extraction

String regex = "(?<=\\?q=)([^(]*)\\(([\\d.]*)";
        String s = "htt://maps.google.com/maps?q=52.258301,+-7.111900+(192.168.159.1Country:Ireland,City:Waterford-by htt://www.javaquery.com)&iwloc=A&hl=en";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(s);
        if (m.find()) {
            System.out.println(m.group(2));
            System.out.println(m.group(1));
        }

outputs:

192.168.159.1
52.258301,+-7.111900+
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks works great, one question the output is 52.258301,+-7.111900+(192.168.159.1 How would I make it so that it becomes (192.168.159.1+52.258301,+-7.111900
as I said, you just take group(2) and group(1), you can arrange the order as your wish. also you can skip the ( . or shell I provide codes for it??
Yes its just the output I get is then used further on in what im doing and it follows the format of IP then the Coordinates, so Im just unsure as to how to flip it around, sorry just never used regular expressions before this
Actually can't thank you enough for your help thanks a million, it works perfectly
0

An approach to extract the two coordinates, without regex, can be:

String str="http://maps.google.com/maps?q=52.258301,+-7.111900+(192.168.159.1Country:Ireland,City:Waterford-by htt://www.javaquery.com)&iwloc=A&hl=en";

int index_x=str.indexOf("?q=")+"?q=".length();
int index_x_end=str.indexOf(",");
int index_y=index_x_end+",".length();
int index_y_end=str.indexOf("+(");

System.out.println(str.substring(index_x, index_x_end));    //prints 52.258301
System.out.println(str.substring(index_y, index_y_end));    //prints +-7.111900

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.