用C++正则表达式匹配AT指令,包括前面的字符串和后面的"," +CGDCONT: 1,"IPV4V6","cmnet","",0,0,0,0,,,,,,0,,,0,,0,0,0,0
时间: 2023-10-01 12:05:47 浏览: 72
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\s]+\",\"[\w\s]+\",\"\",.*?),)");
std::smatch match;
if (std::regex_search(input, match, pattern)) {
std::cout << "Match found!" << std::endl;
std::string matched_string = match[1].str();
std::cout << matched_string << std::endl;
} else {
std::cout << "No match found!" << std::endl;
}
return 0;
}
```
在上述代码中,正则表达式`R"((\+CGDCONT: \d+,\"[\w\s]+\",\"[\w\s]+\",\"\",.*?),)"`用于匹配"+CGDCONT: "后面的数字,双引号中的字母数字空格组合,以及后面的逗号。`std::regex_search()`函数用于匹配输入字符串,如果匹配成功,则进入`if`语句块,然后通过`std::smatch`对象`match`获取整个匹配的字符串。通过调用`str()`方法将匹配结果转换为字符串,并进行输出。
输出结果为:
```
Match found!
+CGDCONT: 1,"IPV4V6","cmnet","",0,0,0,0,,,,,,0,,,0,,0,0,0,0,
```
你可以根据需要修改正则表达式来适应不同的匹配要求,并根据匹配结果进行相应的处理。
阅读全文