我需要帮助为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;
}发布于 2020-01-29 14:15:48
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");https://stackoverflow.com/questions/59968821
复制相似问题