Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QRegExp string matching fails
Qt 6.11 is out! See what's new in the release blog

QRegExp string matching fails

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 5.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    strangelookingnerd
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

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

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • S Offline
        S Offline
        strangelookingnerd
        wrote on last edited by
        #3

        [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!

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            strangelookingnerd
            wrote on last edited by
            #5

            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
            @

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lgeyer
              wrote on last edited by
              #6

              @
              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"); // ""
              @

              1 Reply Last reply
              0
              • S Offline
                S Offline
                strangelookingnerd
                wrote on last edited by
                #7

                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 =)

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved