[英]QT regex display the full text instead of what is asked
i'm having an issue with my regex here where i'm trying to get the longitude of a gps data and instead it's giving me the entire coordinate 我在这里的正则表达式遇到问题,我正在尝试获取gps数据的经度,而是给了我整个坐标
my code 我的密码
QRegularExpression GPSLong("((?<=,\\s)([0-9](.*?)+(W|E)))");
while (!file.atEnd())
{
QString line = file.readLine();
i++;
QRegularExpressionMatch matchGPSLong = GPSLong.match(line);
if ( matchGPSLong.hasMatch())
{
QString GPSLongCoordinates = matchGPSLong.captured(0);
qDebug()<< "Longitude : " <<GPSLongCoordinates;
}
//....
}
An example line
is 示例
line
是
43° 31' 8.3" N, 5° 3' 13.2" E, 0m
43°31'8.3“北,5°3'13.2” E,0m
And the result I'm getting is 我得到的结果是
Longitude : 43° 31' 8.3" N, 5° 3' 13.2" E
经度:43°31'8.3“北,5°3'13.2” E
But I was expecting 但我期待
Longitude : 5° 3' 13.2" E
经度:5°3'13.2“ E
i'm using Qt 5.9.2 on Windows10 and can't update to a newer version because of the software i'm working on 我在Windows10上使用Qt 5.9.2,由于我正在使用的软件而无法更新到较新版本
EDIT : i managed to find a solution, instead of getting DMS(Degrees, Minute, Second) coordinates i switched to DD (Decimal Degrees) and then used global match to obtain a QRegularExpressionMatchIterator
编辑 :我设法找到一种解决方案,而不是获取DMS(度,分,秒)坐标,而是切换到DD(十进制),然后使用全局匹配来获取
QRegularExpressionMatchIterator
here's the fixed and working code : 这是固定的工作代码:
QRegularExpression GPSRegex("-|[0-9]+\\.+[0-9]+");
while (!file.atEnd()) {
QString line = file.readLine();
i++;
QRegularExpressionMatch matchGPSLat = GPSRegex.match(line);
auto matchIterator = CoordinateRegex.globalMatch(line);
if (matchGPSLat.hasMatch()) {
QString GPSLatCoordinates = matchGPSLat.captured(0);
while(matchIterator.hasNext())
{
auto result = matchIterator.next();
if (!matchIterator.hasNext())
{
GPSLongCoordinates = result.captured(0);
}
}
double GPSlat = GPSLatCoordinates.toDouble();
double GPSLong = GPSLongCoordinates.toDouble();
}
}
You want: 你要:
QString GPSLongCoordinates = matchGPSLong.captured(1);
instead of: 代替:
QString GPSLongCoordinates = matchGPSLong.captured(0);
The 0-index of a capture is the entire match. 捕获的0索引是整个匹配项。 If any groups match, those groups starting at index 1 are the respective matches.
如果有任何组匹配,则从索引1开始的那些组是相应的匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.