用C++正则表达式匹配AT指令 +CGDCONT: 1,"IPV4V6","cmnet","",0,0,0,0,,,,,,0,,,0,,0,0,0,0
时间: 2023-10-01 10:05:47 浏览: 220
C++算法:正则表达式匹配
要匹配字符串中的"+CGDCONT: 1,"IPV4V6","cmnet","",0,0,0,0,,,,,,0,,,0,,0,0,0,0",你可以使用以下C++代码:
```cpp
#include <iostream>
#include <regex>
int main() {
std::string input = "+CGDCONT: 1,\"IPV4V6\",\"cmnet\",\"\",0,0,0,0,,,,,,0,,,0,,0,0,0,0";
std::regex pattern(R"(\+CGDCONT: (\d+),\"(\w+)\",\"(\w+)\",\"\",(\d+),(\d+),(\d+),(\d+))");
std::smatch match;
if (std::regex_search(input, match, pattern)) {
std::cout << "Match found!" << std::endl;
std::string group_1 = match[1].str();
std::string group_2 = match[2].str();
std::string group_3 = match[3].str();
std::string group_4 = match[4].str();
std::string group_5 = match[5].str();
std::string group_6 = match[6].str();
std::string group_7 = match[7].str();
std::cout << group_1 << std::endl;
std::cout << group_2 << std::endl;
std::cout << group_3 << std::endl;
std::cout << group_4 << std::endl;
std::cout << group_5 << std::endl;
std::cout << group_6 << std::endl;
std::cout << group_7 << std::endl;
} else {
std::cout << "No match found!" << std::endl;
}
return 0;
}
```
在上述代码中,正则表达式`R"(\+CGDCONT: (\d+),\"(\w+)\",\"(\w+)\",\"\",(\d+),(\d+),(\d+),(\d+))"`用于匹配"+CGDCONT: "后面的数字,双引号中的字母数字组合,以及后面的数字。`std::regex_search()`函数用于匹配输入字符串,如果匹配成功,则进入`if`语句块,然后通过`std::smatch`对象`match`获取每个组的内容。通过调用`str()`方法将匹配结果转换为字符串,并进行输出。
输出结果为:
```
Match found!
1
IPV4V6
cmnet
0
0
0
0
```
你可以根据需要修改正则表达式来适应不同的匹配要求,并根据匹配结果进行相应的处理。
阅读全文