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 help
QtWS25 Last Chance

QRegExp help

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 405 Views
  • 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.
  • I Offline
    I Offline
    Inzinejkr
    wrote on last edited by
    #1

    I'm trying to separate all integers from a string. The string could look like c1:c2?c3 and I would like to get 1, 2, 3. Currently, I have:

     QRegExp xRegExp("[0-9]");
        xRegExp.indexIn(string);
        QStringList list = xRegExp.capturedTexts();
    

    but, it's only capturing the first number. How do I make it capture all occurrences?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      First thing first: are you using Qt 4 ? If not then please move to QRegularExpression as QRegExp has been deprecated and will be removed with Qt6.

      QRegularExpression::match is what you are looking for.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • I Offline
        I Offline
        Inzinejkr
        wrote on last edited by
        #3

        @SGaist said in QRegExp help:

        QRegularExpression::match is what you are looking for.

        Cheers, solved by:

            QRegularExpression rx("[0-9]");
            QRegularExpressionMatchIterator i = rx.globalMatch(string);
            int z=1;
            static int nums[6]={0, 0, 0, 0, 0, 0};
            while (i.hasNext()) {
                QRegularExpressionMatch match = i.next();
                nums[z++] = match.captured(0).toInt();
            }
            return nums;
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You know that:

          • as soon as your string contains more than 5 numbers you are going to crash ?
          • if it contains less that 6 number on a second pass it's going to contain old values ?
          • that the first value will always be 0 because z start at one

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          I 1 Reply Last reply
          4
          • SGaistS SGaist

            You know that:

            • as soon as your string contains more than 5 numbers you are going to crash ?
            • if it contains less that 6 number on a second pass it's going to contain old values ?
            • that the first value will always be 0 because z start at one
            I Offline
            I Offline
            Inzinejkr
            wrote on last edited by
            #5
            • as soon as your string contains more than 5 numbers you are going to crash ?

            It never will.

            • that the first value will always be 0 because z start at one

            That is intended.

            • it's going to contain old values ?

            Cheers for that one.

            SGaistS 1 Reply Last reply
            0
            • I Inzinejkr
              • as soon as your string contains more than 5 numbers you are going to crash ?

              It never will.

              • that the first value will always be 0 because z start at one

              That is intended.

              • it's going to contain old values ?

              Cheers for that one.

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Inzinejkr said in QRegExp help:

              as soon as your string contains more than 5 numbers you are going to crash ?

              It never will.

              Then you should add an assert that ensures that. Never has a tendency to not hold when programming. A comment would be nice as well because the future you or person who's going to inherit that code might have to do spelunking to fully understand why it was implemented that way.

              @Inzinejkr said in QRegExp help:

              that the first value will always be 0 because z start at one

              That is intended.

              Again, put a comment that says so for the same reason as above.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2

              • Login

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