使用 C++ 验证 NMEA 句子



我需要帮助为 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

我还包含了我尝试过的表达方式,我在网上找到了它。但是当我运行它时,它说未知的转义序列。

#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;
}

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

将反斜杠加倍:

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

或使用原始文本:

regex r(R"re([A-Z] w+,d,d,(?:d{1}|),[A-B],[^,]+,0*([A-Za-z0-9]{2}))re");

最新更新