嗨,我有一个 Java 程序,它运行并查找我的机器的经度和纬度坐标,它将其输出为如下所示的长字符串,
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
我现在想做的只是从这个字符串中提取: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/