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. QStringList::indexOf(QRegularExpression) don't work :(
Qt 6.11 is out! See what's new in the release blog

QStringList::indexOf(QRegularExpression) don't work :(

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 960 Views 2 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
    Stefanoxjx
    wrote on last edited by
    #1

    Hi, I've a problem searching a part of string in QStringList.
    I've this QStringList:

    QStringList *Choices = new QStringList;
    
        Choices->append("Languages|1|Italian");
        Choices->append("Languages|2|English");
        Choices->append("Languages|3|Spanish");
        Choices->append("Languages|4|Japanese");
        Choices->append("City|0|New York");
        Choices->append("City|1|Toronto");
        Choices->append("City|2|Rome");
        Choices->append("Color|2|Red");
        Choices->append("Color|1|Bronwn");
        Choices->append("Color|5|Black");
        Choices->append("Name|2|Stefano");
        Choices->append("Name|2|John");
        Choices->append("Name|2|Jack");
    

    and I must to search first occurence of an initial part of string.
    To obtain this result, I use this code:

    int16_t idx = int16_t(slChoices->indexOf(QRegularExpression("^Color")));
    

    but result is always -1 :(
    I don't understand where I'm wrong.
    If I test my regular expression here: https://www.debuggex.com/ it work fine.

    Can you help me?
    Thanks.

    Stefano

    KroMignonK 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      See the documentation: Returns the index position of the first exact match.

      btw: There is no need to create a QStringList on the heap.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      4
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        Are you looking for Color in the beginning of the string ?
        I was wondering if you can use
        https://doc.qt.io/qt-5/qstring.html#startsWith

        1 Reply Last reply
        1
        • S Stefanoxjx

          Hi, I've a problem searching a part of string in QStringList.
          I've this QStringList:

          QStringList *Choices = new QStringList;
          
              Choices->append("Languages|1|Italian");
              Choices->append("Languages|2|English");
              Choices->append("Languages|3|Spanish");
              Choices->append("Languages|4|Japanese");
              Choices->append("City|0|New York");
              Choices->append("City|1|Toronto");
              Choices->append("City|2|Rome");
              Choices->append("Color|2|Red");
              Choices->append("Color|1|Bronwn");
              Choices->append("Color|5|Black");
              Choices->append("Name|2|Stefano");
              Choices->append("Name|2|John");
              Choices->append("Name|2|Jack");
          

          and I must to search first occurence of an initial part of string.
          To obtain this result, I use this code:

          int16_t idx = int16_t(slChoices->indexOf(QRegularExpression("^Color")));
          

          but result is always -1 :(
          I don't understand where I'm wrong.
          If I test my regular expression here: https://www.debuggex.com/ it work fine.

          Can you help me?
          Thanks.

          Stefano

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #4

          @Stefanoxjx said in QStringList::indexOf(QRegularExpression) don't work :(:

          To obtain this result, I use this code:
          int16_t idx = int16_t(slChoices->indexOf(QRegularExpression("^Color")));

          As @Christian-Ehrlicher has written, you need exact match for QStringList::indexOf(), so you have to change you regex to:

          int16_t idx = int16_t(slChoices->indexOf(QRegularExpression("^Color.*")));
          

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          JonBJ 1 Reply Last reply
          4
          • KroMignonK KroMignon

            @Stefanoxjx said in QStringList::indexOf(QRegularExpression) don't work :(:

            To obtain this result, I use this code:
            int16_t idx = int16_t(slChoices->indexOf(QRegularExpression("^Color")));

            As @Christian-Ehrlicher has written, you need exact match for QStringList::indexOf(), so you have to change you regex to:

            int16_t idx = int16_t(slChoices->indexOf(QRegularExpression("^Color.*")));
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @KroMignon
            In which case, to confirm my understanding of https://doc.qt.io/qt-5/qstringlist.html#indexOf-4 wording which I don't find very clear, the ^ is redundant and QRegularExpression("Color.*") would match just as well? The match is against each string in the string list individually, and the regular expression is treated as though it were anchored with ^...$?

            KroMignonK 1 Reply Last reply
            1
            • JonBJ JonB

              @KroMignon
              In which case, to confirm my understanding of https://doc.qt.io/qt-5/qstringlist.html#indexOf-4 wording which I don't find very clear, the ^ is redundant and QRegularExpression("Color.*") would match just as well? The match is against each string in the string list individually, and the regular expression is treated as though it were anchored with ^...$?

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #6

              @JonB said in QStringList::indexOf(QRegularExpression) don't work :(:

              The match is against each string in the string list individually, and the regular expression is treated as though it were anchored with ^...$?

              Yes, this is the way QStringList::indexOf(onst QRegularExpression &re, int from = 0) is working.

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

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

                @KroMignon : Many thanks, it works fine :)
                obviously many thanks also to all the others who answered me :)

                1 Reply Last reply
                1

                • Login

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