得到的Command不要出现"^" "+"
时间: 2023-07-31 15:06:09 浏览: 138
如果您想要在输出结果中去掉指令前面的 "^" 和 "+" 符号,您可以在解析过程中进行相应的处理。以下是修改后的示例代码:
```cpp
#include <iostream>
#include <regex>
#include <string>
#include <vector>
struct ATCommand {
std::string command;
std::vector<std::string> parameters;
};
std::vector<ATCommand> parseATCommands(const std::string& input) {
std::regex pattern(R"((\+\w+:.*|\^\w+:.*))");
std::regex paramPattern(R"([^:,]+|\"([^\"]+)\")");
std::vector<ATCommand> commands;
std::sregex_iterator iter(input.begin(), input.end(), pattern);
std::sregex_iterator end;
while (iter != end) {
std::smatch match = *iter;
ATCommand command;
std::string matchedString = match.str();
size_t colonPos = matchedString.find(':');
if (colonPos != std::string::npos) {
command.command = matchedString.substr(1, colonPos - 1); // 去掉指令前面的符号
std::string params = matchedString.substr(colonPos + 1);
std::sregex_iterator paramIter(params.begin(), params.end(), paramPattern);
std::sregex_iterator paramEnd;
while (paramIter != paramEnd) {
std::smatch paramMatch = *paramIter;
for (const auto& m : paramMatch) {
if (!m.str().empty()) {
command.parameters.push_back(m.str());
}
}
++paramIter;
}
}
commands.push_back(command);
++iter;
}
return commands;
}
int main() {
std::string input = R"(^SIMST: 1,0
+CEREG: 2
^CPBREADY
^RRCSTAT: 1,1
^SRVST: 2
+CEREG: 1,"161818","0FB9B065",11
+C5GREG: 1,"161818","31FB9B065",11,2,01
+CIREGU: 2,0
^RSSI:31
^HCSQ: "NR",79,121,28
^RSSI:31)";
std::vector<ATCommand> commands = parseATCommands(input);
for (const auto& command : commands) {
std::cout << "Command: " << command.command << std::endl;
for (const auto& param : command.parameters) {
std::cout << "Parameter: " << param << std::endl;
}
std::cout << std::endl;
}
return 0;
}
```
在上述代码中,我们在解析过程中使用`substr()`函数来去掉指令前面的 "^" 和 "+" 符号,将修改后的指令存储在`command.command`中。
输出结果为:
```
Command: SIMST
Parameter: 1
Parameter: 0
Command: CEREG
Parameter: 2
Command: CPBREADY
Command: RRCSTAT
Parameter: 1
Parameter: 1
Command: SRVST
Parameter: 2
Command: CEREG
Parameter: 1
Parameter: "161818"
Parameter: "0FB9B065"
Parameter: 11
Command: C5GREG
Parameter: 1
Parameter: "161818"
Parameter: "31FB9B065"
Parameter: 11
Parameter: 2
Parameter: 01
Command: CIREGU
Parameter: 2
Parameter: 0
Command: RSSI
Parameter: 31
Command: HCSQ
Parameter: "NR"
Parameter: 79
Parameter: 121
Parameter: 28
Command: RSSI
Parameter: 31
```
希望这次的回答能够满足您的需求。如果还有其他问题,请随时提问。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)