[Qt-interest] Problem with RegExp extraction value from Json kind of response

Denis Akhmetzyanov dakhmetzyanov at smartlabs.tv
Thu Mar 25 10:08:01 CET 2010


Hello!
You should move '?' out of the parentheses (i.e. 'one or zero key') and
remove '\' before ':' (because this 'unknown escape sequence'). Also set
QRegExp::setMinimal(true) - because QRegExp is greedy by default and matched
all characters until last comma. Then code:

    QString http_response =
"{\"key\":\"a99fdd865c2-10000\",\"oid\":1000055,\"expires\":0000,\"secret\":\"509c03edfdc7\",\"sign\":\"f0dd9e5226d0e77\"}";
    // or even if i use QRegExp::escape on the string it dosn't work
    QRegExp rx("\"key\":(.*)?\",");
    rx.setMinimal(true);
    int yy = rx.indexIn(http_response);
    QString cap = rx.cap(1);

    qDebug() << yy; // prints '1'
    qDebug() << cap; // prints '"a99fdd865c2-10000'


2010/3/25 Meir Yanovich <meiry242 at gmail.com>

> Hello all
> im trying to extract value by key from http Json kind of struct by  using
> simple RegExp
> here is my code i like to extract the key value from the string:
>
> QString http_response =
> "{\"key\":\"a99fdd865c2-10000\",\"oid\":1000055,\"expires\":0000,\"secret\":\"509c03edfdc7\",\"sign\":\"f0dd9e5226d0e77\"}";
> // or even if i use QRegExp::escape on the string it dosn't work
> QRegExp rx("\"key\"\:(.*?)\",");
> int yy = rx.indexIn(http_response);
> QString cap = rx.cap(1);
>
> but i cant capture the string and the yy variable always gives me -1
> i tested this regexp in this regexp tester : http://gskinner.com/RegExr/and its working fine
> what im doing wrong here ?
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100325/8451d3fe/attachment.html 


More information about the Qt-interest-old mailing list