例如,我有一个GPS记录器输出GPGGA字符串
$GPGGA,1714.0,3723.465874,N,12202.26954,W,2,6,1.2,18.893,M,-25.669,M,2.0,0031*4F
该字符串以逗号分隔。在Java中工作,我试图提取位置信息。有人可以帮助我使用所需的正则表达式语句
3723.465874
在第2个和第3个逗号之间N
在第3个和第4个逗号之间12202.26954
在第4个和第5个逗号之间W
在第5个和第6个逗号之间或者,或许只是一个例子,所以我可以推断其他人。
谢谢。
答案 0 :(得分:1)
只需在逗号上拆分字符串,然后从结果字符串数组中获取相应的部分。
String gpgga =
"$GPGGA,1714.0,3723.465874,N,12202.26954,W,2,6,1.2,18.893,M,-25.669,M,2.0,0031*4F"
String[] parts = gpgga.split(",");
String latitude = parts[2];
String nb = parts[3];
String longitude = parts[4];
String ew = parts[5];
答案 1 :(得分:1)
由于您有一个干净的分隔符,您可以使用String.split
,如下所示:
String data = "$GPGGA,1714.0,3723.465874,N,12202.26954,W,2,6,1.2,18.893,M,-25.669,M,2.0,0031*4F";
String[] parts = data.split(",");
String p2 = parts[2]; // 3723.465874
String p3 = parts[3]; // N
String p4 = parts[4]; // 12202.26954
String p5 = parts[5]; // W
答案 2 :(得分:1)
来自this gist:
package es.agroguia.model;
import java.util.HashMap;
import java.util.Map;
public class NMEA {
class GPSPosition {
public float time = 0.0f;
public float lat = 0.0f;
public float lon = 0.0f;
public boolean fixed = false;
public int quality = 0;
public float dir = 0.0f;
public float altitude = 0.0f;
public float velocity = 0.0f;
public void updatefix() {
fixed = quality > 0;
}
public String toString() {
return String.format("POSITION: lat: %f, lon: %f, time: %f, Q: %d, dir: %f, alt: %f, vel: %f", lat, lon, time, quality, dir, altitude, velocity);
}
}
GPSPosition position = new GPSPosition();
private static final Map<String, SentenceParser> sentenceParsers = new HashMap<String, SentenceParser>();
public NMEA() {
sentenceParsers.put("GPGGA", new GPGGA());
sentenceParsers.put("GPGGL", new GPGGL());
sentenceParsers.put("GPRMC", new GPRMC());
sentenceParsers.put("GPRMZ", new GPRMZ());
//only really good GPS devices have this sentence but ...
sentenceParsers.put("GPVTG", new GPVTG());
}
public GPSPosition parse(String line) {
if(line.startsWith("$")) {
String nmea = line.substring(1);
String[] tokens = nmea.split(",");
String type = tokens[0];
//TODO check crc
if(sentenceParsers.containsKey(type)) {
sentenceParsers.get(type).parse(tokens, position);
}
position.updatefix();
}
return position;
}
}
interface SentenceParser {
public boolean parse(String [] tokens, GPSPosition position);
}
// utils
static float Latitude2Decimal(String lat, String NS) {
float med = Float.parseFloat(lat.substring(2))/60.0f;
med += Float.parseFloat(lat.substring(0, 2));
if(NS.startsWith("S")) {
med = -med;
}
return med;
}
static float Longitude2Decimal(String lon, String WE) {
float med = Float.parseFloat(lon.substring(3))/60.0f;
med += Float.parseFloat(lon.substring(0, 3));
if(WE.startsWith("W")) {
med = -med;
}
return med;
}
// parsers
class GPGGA implements SentenceParser {
public boolean parse(String [] tokens, GPSPosition position) {
position.time = Float.parseFloat(tokens[1]);
position.lat = Latitude2Decimal(tokens[2], tokens[3]);
position.lon = Longitude2Decimal(tokens[4], tokens[5]);
position.quality = Integer.parseInt(tokens[6]);
position.altitude = Float.parseFloat(tokens[9]);
return true;
}
}
}
答案 3 :(得分:1)
String input = "$GPGGA,1714.0,3723.465874,N,12202.26954,W,2,6,1.2,18.893,M,-25.669,M,2.0,0031*4F";
Pattern pattern = Pattern.compile("^\\$GPGGA,[\\d\\.]*,([\\d\\.]+),([NS]),([\\d\\.]+),([EW]),.*");
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
for(int i=0; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + " = " + matcher.group(i));
}
} else {
throw new IllegalArgumentException("Invalid input!");
}
输出:
Group 0 = $GPGGA,1714.0,3723.465874,N,12202.26954,W,2,6,1.2,18.893,M,-25.669,M,2.0,0031*4F
Group 1 = 3723.465874
Group 2 = N
Group 3 = 12202.26954
Group 4 = W