嗨,我有一个Java程序,可以运行并找到我的机器的经度和纬度坐标,并将其输出为长字符串,如下所示,


htt://maps.google.com/maps?q = 52.258301,+-7.111900+(192.168.159.1国家/地区:爱尔兰,城市:沃特福德
htt://www.javaquery.com)&iwloc = A&hl = zh-CN


我现在想做的只是从此字符串中提取:IP地址和两个坐标,我已经成功获取了IP地址,但似乎无法获得两个坐标。
最终结果有望是


192.168.159.1,52.258301,+-7.111900


到目前为止,我使用这些表达式来获取IP地址

(?:(?: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]?)


哪个很好
然后尝试使用此获取坐标

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


但它只会得到第一个坐标,然后再次

谢谢

最佳答案

尝试使用此正则表达式:

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


group(1)52.258301,+-7.111900+

group(2) is the ip


编辑为正则表达式匹配/提取添加代码

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


输出:

192.168.159.1
52.258301,+-7.111900+

关于java - 从Java中的字符串中提取IP和坐标,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15226670/

10-14 18:49
查看更多