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. How to get the regular expression matched part
Forum Updated to NodeBB v4.3 + New Features

How to get the regular expression matched part

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 595 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.
  • MihanM Offline
    MihanM Offline
    Mihan
    wrote on last edited by
    #1

    Hi
    I want to get the address like IP from a string.
    e.g. Device address: 192.168.1.1
    Local address: 192.168.1.2

    I have gotten a regular expression QRegExp("(\\d{1,3}\\.){3}\\d{1,3}") to match address.
    So how to use this regular expression to get the address, not use it to spilt.

    Regards
    Mihan

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

      See QRegularExpression::match()

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

      MihanM 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        See QRegularExpression::match()

        MihanM Offline
        MihanM Offline
        Mihan
        wrote on last edited by
        #3

        @Christian-Ehrlicher Thanks, QRegularExpression is something I haven't known.

        What's the different of QRegExp and QRegularExpression

        B JonBJ 2 Replies Last reply
        0
        • MihanM Mihan

          @Christian-Ehrlicher Thanks, QRegularExpression is something I haven't known.

          What's the different of QRegExp and QRegularExpression

          B Offline
          B Offline
          Bonnie
          wrote on last edited by Bonnie
          #4

          @Mihan You can also get the address by QRegExp.

          QString device="Device address: 192.168.1.1";
          QRegExp rx("(\\d{1,3}\\.){3}\\d{1,3}");
          if(rx.indexIn(device) >= 0) {
              qDebug() << rx.cap();
          }
          

          As the doc says

          Note: In Qt 5, the new QRegularExpression class provides a Perl compatible implementation of regular expressions and is recommended in place of QRegExp.

          You can check this page for the difference of the two classes:
          https://doc.qt.io/qt-5/qregularexpression.html#notes-for-qregexp-users

          1 Reply Last reply
          2
          • MihanM Mihan

            @Christian-Ehrlicher Thanks, QRegularExpression is something I haven't known.

            What's the different of QRegExp and QRegularExpression

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @Mihan
            There's a reference somewhere which could be dug out if you want (edit Oh I see @Bonnie has provided one), but in a nutshell QRegExp has been superseded by QRegularExpression and you should use the latter now.

            MihanM 1 Reply Last reply
            3
            • JonBJ JonB

              @Mihan
              There's a reference somewhere which could be dug out if you want (edit Oh I see @Bonnie has provided one), but in a nutshell QRegExp has been superseded by QRegularExpression and you should use the latter now.

              MihanM Offline
              MihanM Offline
              Mihan
              wrote on last edited by
              #6

              @JonB @Bonnie
              Thank you so much.

              Now I have replaced all the QRegExp with QRegularExpression.

              Best regards
              Mihan

              KroMignonK 1 Reply Last reply
              0
              • MihanM Mihan

                @JonB @Bonnie
                Thank you so much.

                Now I have replaced all the QRegExp with QRegularExpression.

                Best regards
                Mihan

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

                @Mihan said in How to get the regular expression matched part:

                Now I have replaced all the QRegExp with QRegularExpression.

                By the way QRegExp has been deprecated by Qt => QTBUG-72974

                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
                0

                • Login

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