Android 从短信字符串中拆分文本/数字
Android split Text / Numbers from SMS string
我正在制作一个 gps 跟踪程序,我收到了一条包含以下内容的短信
smsMessage="http://maps.google.com/maps?q=42.396068,13.45201,17 phone is outside the area"
谁能告诉我如何从短信字符串中只分离出需要的经度和纬度?
提前致谢
您需要正则表达式才能从给定的文本中获取经纬度。这是:
[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)
匹配
- +90.0, -127.554334 45, 180
- -90, -180
- -90.000, -180.0000
- +90, +180
- 47.1231231, 179.99999999
不匹配
- -90., -180.
- +90.1, -100.111
- -91, 123.456
- 045、180
来源:Regular expression for matching latitude/longitude coordinates?
你可以这样做,
String s = "http://maps.google.com/maps?q=42.396068,13.45201,17 phone is outside the area";
String parts[] = s.replaceAll("^.*?=|\s.*", "").split(",(?=[-+]?\d+\.\d+)");
System.out.println(Arrays.toString(parts));
输出:
[42.396068, 13.45201,17]
我正在制作一个 gps 跟踪程序,我收到了一条包含以下内容的短信
smsMessage="http://maps.google.com/maps?q=42.396068,13.45201,17 phone is outside the area"
谁能告诉我如何从短信字符串中只分离出需要的经度和纬度?
提前致谢
您需要正则表达式才能从给定的文本中获取经纬度。这是:
[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)
匹配
- +90.0, -127.554334 45, 180
- -90, -180
- -90.000, -180.0000
- +90, +180
- 47.1231231, 179.99999999
不匹配
- -90., -180.
- +90.1, -100.111
- -91, 123.456
- 045、180
来源:Regular expression for matching latitude/longitude coordinates?
你可以这样做,
String s = "http://maps.google.com/maps?q=42.396068,13.45201,17 phone is outside the area";
String parts[] = s.replaceAll("^.*?=|\s.*", "").split(",(?=[-+]?\d+\.\d+)");
System.out.println(Arrays.toString(parts));
输出:
[42.396068, 13.45201,17]