• 5785阅读
  • 5回复

[提问]提取经纬度的正则表达式如何写? [复制链接]

上一主题 下一主题
离线lipper
 
只看楼主 倒序阅读 楼主  发表于: 2012-03-14
points.push(new GLatLng(64.084539881516,-21.953430550266));如何用正则表达式将其中的经纬度数据匹配并提取出来呢???
离线lipper
只看该作者 1楼 发表于: 2012-03-14
当然可以投机取巧了,比如

    QString pattern("(.*),(.*)");
    QRegExp rx(pattern);
        QString line = in.readLine();
        //----process_line(line);
        line.replace("points.push(new GLatLng(","");
        line.replace("));","");
        int pos = line.indexOf(rx);     // 0, position of the first match.

【ps】如果不做替换该怎么写表达式呢?谢谢啊……
离线wxj120bw

只看该作者 2楼 发表于: 2012-03-14
回 1楼(lipper) 的帖子
([-]*[0-9]{2,3}.[0-9]+),([-]*[0-9]{2,3}.[0-9]+)
PS:没有验证
离线lipper
只看该作者 3楼 发表于: 2012-03-17
QString pattern("([-]*[0-9]{2,3}.[0-9]+),([-]*[0-9]{2,3}.[0-9]+)");
QRegExp rx(pattern);
QString line = "points.push(new GLatLng(64.084539881516,-21.953430550266));";
int pos = line.indexOf(rx);     // 0, position of the first match.
qreal lat = rx.cap(1).toDouble();
qreal lng = rx.cap(2).toDouble();

非常感谢,按wxj120bw的办法搞定了!
离线wxj120bw

只看该作者 4楼 发表于: 2012-03-17
回 3楼(lipper) 的帖子
经度的数值范围我弄错了 你稍微改下
离线lipper
只看该作者 5楼 发表于: 2012-03-19
QString pattern("([-]*[0-9]{1,3}.[0-9]+),([-]*[0-9]{1,3}.[0-9]+)");
改成{1,3}就好了

谢谢关注
快速回复
限100 字节
 
上一个 下一个