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. [Solved] Simple Regex assistance
QtWS25 Last Chance

[Solved] Simple Regex assistance

Scheduled Pinned Locked Moved General and Desktop
regexqt5
7 Posts 3 Posters 2.2k 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.
  • J Offline
    J Offline
    justin1122
    wrote on last edited by justin1122
    #1

    I've been trying to capture some digits within a string from the output of fdisk. Here is the input string:

    QString s = Disk /dev/mmcblk2: 3.6 GiB, 3867148288 bytes, 7553024 sectors

    My target capture is the 3.6 GiB number. I have been trying:

    QRegExp rx("^\b*\bG");
    rx.indexIn(s);
    qDebug() << "Captured: " + rx.cap(0);

    I figured that I could apply a word/letter boundary on the colon or partition location and then another boundary on the GiB string.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by mcosta
      #2

      HI,

      your RegExp is wrong

      this program

      #include <QCoreApplication>
      
      #include <QtDebug>
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          QString s = "Disk /dev/mmcblk2: 3.6 GiB, 3867148288 bytes, 7553024 sectors";
      
          QRegExp rx("([\\d\\.]+) GiB");
          qDebug() << rx.indexIn(s);
          qDebug() << "Captured: " + rx.cap(1);
      }
      

      works as you expect.

      NOTE: rx.cap(0) returns the whole string matching the RegExp; rx.cap(1) the captured group ()

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

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

        Hi,

        When dealing with QRegExp you can use the regexp example in examples/tools. It's a simple yet pretty nice application to test your regexp.

        If you are using Qt 5, you should consider QRegularExpression. Since Qt 5.5 there's also the helper tool that you can use to build your regexp.

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

        M 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          When dealing with QRegExp you can use the regexp example in examples/tools. It's a simple yet pretty nice application to test your regexp.

          If you are using Qt 5, you should consider QRegularExpression. Since Qt 5.5 there's also the helper tool that you can use to build your regexp.

          M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          @SGaist said:

          Since Qt 5.5 there's also the helper tool that you can use to build your regexp.

          What's that tool??

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

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

            The name's "regularexpression" but after a double check, it might have missed the 5.5 window, it's in the dev branch here but compilable without any problem with "older" releases

            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
            0
            • M Offline
              M Offline
              mcosta
              wrote on last edited by
              #6

              ok thanks, is an example

              Once your problem is solved don't forget to:

              • Mark the thread as SOLVED using the Topic Tool menu
              • Vote up the answer(s) that helped you to solve the issue

              You can embed images using (http://imgur.com/) or (http://postimage.org/)

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

                I'd say the folder name is a bit misleading in this case

                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
                0

                • Login

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