以下是从客户端发送并由服务器接收的消息示例。
"P_ID:[1]MSG:[This is a test message...]"
如何有效提取Server端P_ID:[]
& MSG:[]
之间的信息,并去除信息中的括号?
例如:
转动 MSG:[这是一条测试消息...]
进入这是一条测试消息...
最佳答案
第一个也是最明显的方法是 string::find ("MSG:[") 然后是 string::substr () 得到其余的。
类似的东西:
size_t start = data.find("MSG:[");
std::string msg = data.substr(start, data.find("]", start) - start);
但在 c++11 中更优雅和灵活的方法是通过 std::regex_match 在正则表达式上使用正则表达式 MSG:\[(.*)\]
关于c++ - 从字符串中提取多个子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32686601/