c++ - 从 query_string 中读取值

标签 c++

我想从 query_string 中读取数据。

例如:

username=kramsp&password=overfloww123

我想从查询字符串中读取值

query= username=[data1] password=[data2]
//where data1, data2 are values

char *username = [data1]; // kramster
char *password = [data2]; // overfloww123

最佳答案

//使用 C++(使用正则表达式)解析接收到的字符串

regex e("username=([[:w:]]+)&password=([[:w:]]+)");

string test = "username=kramsp&password=overfloww123";

smatch m;
regex_search(test, m, e);

if (m.size() > 2)
{
    const char *username = m[1].str().c_str();
    const char *password = m[2].str().c_str();

    cout << username << endl;
    cout << password << endl;
}

DEMO

关于c++ - 从 query_string 中读取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30573012/

相关文章:

c++ - 在 Windows 7 上使用 SHA2-512 (CALG_SHA_512) 返回 "Invalid Algorithm Specified"

c++ - 映射内映射(映射作为键)

C++ Shader矩阵问题

c++ - 为什么 Windows 不允许在模拟另一个用户时启动 WinSock

c++ - 标题栏中的汉字

c++ - g++ 错误 : call of overloaded 'abs(unsigned int)' is ambiguous

c++ - 类继承 : Function is inaccessible

c++ - VS2013 : How to disable warnings for included header files outside of the project

c++ - 使用模板参数作为模板参数

c++ - 将 QModelIndex 转换为 QString