首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用C++验证NMEA语句

用C++验证NMEA语句
EN

Stack Overflow用户
提问于 2020-01-29 14:06:48
回答 1查看 423关注 0票数 0

我需要帮助为NMEA语句创建正则表达式。之所以如此,是因为我想验证数据是否是NMEA句子的正确形式。使用C++。下面是GLL形式的NMEA句子的一些例子。如果可能的话,我还想获得一个c++示例,它将验证代码。

$GPGLL,5425.32,N,106.92,W,82808*64

$GPGLL,5425.33,N,106.91,W,82826*6a

$GPGLL,5425.32,N,106.9,W,82901*5e

$GPGLL,5425.32,N,106.89,W,82917*61

我还包括了我在网上找到的表达方式。但当我运行它的时候,上面写着未知的逃逸顺序。

代码语言:javascript
复制
#include <iostream>
#include <regex>
#include<string.h>
using namespace std;

int main()
{
    // Target sequence
    string s = "$GPGLL, 54 30.49, N, 1 06.74, W, 16 39 58 *5E";

    // An object of regex for pattern to be searched
    regex r("[A-Z] \w+,\d,\d,(?:\d{1}|),[A-B],[^,]+,0\*([A-Za-z0-9]{2})");

    // flag type for determining the matching behavior
    // here it is for matches on 'string' objects
    smatch m;

    // regex_search() for searching the regex pattern
    // 'r' in the string 's'. 'm' is flag for determining
    // matching behavior.
    regex_search(s, m, r);

    // for each loop
    for (auto x : m)
        cout << "The nmea sentence is correct ";

    return 0;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-29 14:15:48

C++编译器将\d和朋友解释为字符转义代码。

或者将反斜杠加倍:

代码语言:javascript
复制
regex r("[A-Z] \\w+,\\d,\\d,(?:\\d{1}|),[A-B],[^,]+,0\\*([A-Za-z0-9]{2})");

或者使用原始文字:

代码语言:javascript
复制
regex r(R"re([A-Z] \w+,\d,\d,(?:\d{1}|),[A-B],[^,]+,0\*([A-Za-z0-9]{2}))re");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59968821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档