QRegExp string matching fails



  • Hey there

    I got a QStringList as follows:
    @
    [0] "DC07 B 1 "2""
    [1] "DC07 B 2 "Test""
    [2] "DC07 A 3"
    @
    Trying to find an index of a specific string in this list i use
    @
    QRegExp("DC07 B 1 "*"", Qt::CaseSensitive, QRegExp::Wildcard)
    @
    which SHOULD give me the first item in the QStringList - but it won't work out!

    Some code:
    @
    QStringList stringList = response.split("\r\n"); // response from a rs232-device
    int index = stringList.indexOf(QRegExp("DC07 B 1 "*"", Qt::CaseSensitive, QRegExp::Wildcard)); // returns -1
    QString result = stringList.at(index); // Some more stuff which doesn't really matter
    @

    Since i have other similar QRegExp-matchings which actually work just like this, I'm a little confused and ask for you to help me :)

    so long


  • Moderators

    Did you try to double your back slashes? Like:
    @
    QRegExp("DC07 B 1 \"*\"", Qt::CaseSensitive, QRegExp::Wildcard)
    @



  • [quote author="koahnig" date="1325598584"]Did you try to double your back slashes? Like:
    @
    QRegExp("DC07 B 1 \"*\"", Qt::CaseSensitive, QRegExp::Wildcard)
    @
    [/quote]

    Won't compile since \"*\" is no legit escape sequence.
    I even tried @QRegExp("DC07 B 1 *", Qt::CaseSensitive, QRegExp::Wildcard)@ wich return -1 aswell!



  • Your patterns looks fine (both, although you can't match "DC07 A 3", as it has no quotes, just use "DC07 A 3*" instead), so your string list is most probably empty.



  • Nope, i checked that - it isn't empty.
    Other patterns work like a charm, mostly same syntax just looking for other key words.

    Just to clear the reason for matching: the device may change the order of its output-lines which I'm trying to match, for getting a specific information. Thats why I can't just take a constant index of the QStringList. The actual information I need is the number in quotes (in my example it's 2).

    Edit: Just tried something which confuses me even more...

    I took the same QStringList and tried to find my needed string via QString::contains():
    @
    stringList.contains("DC07 A 3"); // returns true
    stringList.contains("DC07 B 1 "2""); // returns false
    stringList.contains("DC07 B 2 "Test""); // returns true
    @



  • @
    QStringList stringList;
    stringList << "DC07 B 1 "2""
    << "DC07 B 2 "Test""
    << "DC07 A 3";

    stringList.indexOf(QRegExp("DC07 B 1 "*"", Qt::CaseSensitive, QRegExp::Wildcard)); // 0
    stringList.indexOf(QRegExp("DC07 B 2 ", Qt::CaseSensitive, QRegExp::Wildcard)); // 1
    stringList.indexOf(QRegExp("DC07 A 3
    ", Qt::CaseSensitive, QRegExp::Wildcard)); // 2
    @

    It would go for regular expressions anyways.
    @
    QString value(const QStringList &list, const QString &id)
    {
    QRegExp pattern(id + " "(.*)"");
    pattern.indexIn(list.value(list.indexOf(pattern)));

    return pattern.cap(1);
    

    }

    value(stringList, "DC07 B 1"); // "2"
    value(stringList, "DC07 B 2"); // "Test"
    value(stringList, "DC07 A 3"); // ""
    @



  • After a long, long way of try and error it somewhat worked. I guess it had something to do with the format the data was recieved from my device. But thank you anyway for your help! Much appreciated =)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.