如上图所示 我想把 “_id”:“0a4ec1f962848a3c04ffdcdb5d66f3ff” 中的 id 值从这么一大长串字符串中提取出来,很简单的一个正则表达式,利用分组获得 0a4ec1f962848a3c04ffdcdb5d66f3ff 在测试工具里面匹配是正常的。
如下代码所示:
QString res = wxHttpOpera::callCloudFunc("getDataBaseSet", var).replace("\\\"", "\"");
//QVariantMap resMap =Tools::StringToVariantMap(Tools::StringToVariantMap(res).value("resp_data").toString());
QRegExp rx("\"_id\":\"(.*)?\"");
int pos = rx.indexIn(res);
if (-1 != pos)
{
QStringList capTexts = rx.capturedTexts();
int ccc=0;
}
相同的东西 搬到 Qt 的 QRegExp 非贪婪模式却无法生效
原因是 Qt 不支持 .*? 这种非贪婪匹配语法 Qt 有自己的非贪婪匹配设置 改成如下代码,逻辑正常
QString res = wxHttpOpera::callCloudFunc("getDataBaseSet", var).replace("\\\"", "\"");
//QVariantMap resMap =Tools::StringToVariantMap(Tools::StringToVariantMap(res).value("resp_data").toString());
QRegExp rx("\"_id\":\"(.*)\"");
rx.setMinimal(true); //设置为非贪婪匹配模式
int pos = rx.indexIn(res);
if (-1 != pos)
{
QStringList capTexts = rx.capturedTexts();
int ccc=0;
}