提取特定字符串

时间:2012-09-11 03:03:26

标签: regex string qt qt4

我有以下QString,我只想提取access_token值,即" http%3a%2f%2fschemas.xmlsoap ....."怎么做?

  

{" token_type":" HTTP://schemas.xmlsoap.org/ws/2009/11/swt-token-profile-1.0","&的access_token #34;:" HTTP%3A%2F%2fschemas.xmlsoap.org%2fws%2f2005%2f05%2fidentity%2fclaims%2fnameidentifier = asdasr21321214a%2F%2fschemas.microsoft.com%2faccwresscontrolservice%2f2010%2f07%2fclaims% 2fidentityprovider = HTTPS%3A%2F%2fdatamarket.accesscontrol.windows.net%2F&安培;观众= HTTP%3A%2F%2fapi.microsofttranslator.com&安培; ExpiresOn = 1347332993&安培;发行人= HTTPS%3A%2F%2fdatamarket.accesscontrol.windows。净%2F&安培; HMACSHA256 = sFqvp2a2xXc3VBdxYZ6xHQf%2fKkOydnuX6VK7A6Yf55k%3D"" expires_in":" 599""范围":" HTTP:// api.microsofttranslator.com"}

3 个答案:

答案 0 :(得分:2)

http://qjson.sourceforge.net/查看QJson。

您可以轻松地将字符串解析为标记化属性。从使用页面:

QJson::Parser parser;
QString json = "{your json above}";
bool ok;
QVariant result = parser.parse (json, &ok);
qDebug() << result["access_token"].toString();

玩得开心。

答案 1 :(得分:1)

试试这个正则表达式:

\"access_token\":\"([^\"]*)\"

解释

<强> ( subexpression )
Captures the matched subexpression and assigns it a zero-based ordinal number.

<强> [^ character_group ]
Negation: Matches any single character that is not in character_group. By default, characters in character_group are case-sensitive.

答案 2 :(得分:1)

快速又脏,仅用于演示QString :: section:

QString Data("{...you json data...}");
QString AccessToken = data.section("access_token\":\"",1).section("\"",0,0);

考虑:QString :: section缓慢而且很重,创建QStringList并在后台将tokens转换为RegExp。但在某些情况下我仍经常使用它。