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. QRegularExpressionMatch
Forum Updated to NodeBB v4.3 + New Features

QRegularExpressionMatch

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 5 Posters 5.5k Views 3 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.
  • Joel BodenmannJ Offline
    Joel BodenmannJ Offline
    Joel Bodenmann
    wrote on last edited by Joel Bodenmann
    #4

    Hi Scott,

    This is something that is definitely possible (using a regex). Regex supports loops and stuff (repeating capturing groups).
    I'm happy to help with this when I get a few minutes but it will be very difficult without a certain set of sample data. Is that something you can share/publish?

    I didn't really try this but something like this should already match one of the instruments: Instrument(\d+)=(-?)(\d+\.\d+) so it's a matter of putting together a capturing group of that and repeating it.

    Industrial process automation software: https://simulton.com
    Embedded Graphics & GUI library: https://ugfx.io

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

      Hi,

      To add to @Joel-Bodenmann, can you show an example of what you want to grab ? That would help devise a proper expression.

      Note that there's the QRegularExpression tool example that you can use to validate that your expression is working.

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

        Here are two example lines containing data. I had to sanitize it for posting - the instrument names are actually very specific and have mostly to do with pressures and temperatures - changing the pattern for the specific names I think I can handle. The amount of data in a line (number of reporting instruments) can actually be much longer - I shortened them for brevity:

        2017-10-19 14:08:58,325 TRACE [Publisher] C.DataPublisher [DataPublisher.java:159] Publishing {Instrument12=-900.0, Instrument0=82.4, Instrument11=-900.0, Instrument1=131.16875000000005, Instrument2=1.0, Instrument13=0.0, Instrument3=120.091247064, Instrument6=-0.9, Instrument22, Instrument4=83.91875000000005, Instrument12Status=1.0, Instrument13Status=1.0}
        2017-10-19 14:08:58,825 TRACE [Publisher] C.DataPublisher [DataPublisher.java:159] Publishing {Instrument0=82.4, Instrument11=-900.0, Instrument1=135.33125000000007, Instrument3=120.091247064, Instrument4=83.91875000000005, Instrument22, Instrument23=-900.0, Instrument31=-900.0}

        Each new line starts with a date and timestamp.

        -Scott

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #7

          If they are not nested (i.e. you never have {Instrument0=82.4,{Instrument11=-900.0, Instrument1=135.33125000000007}, Instrument4=83.91875000000005}) then you can use:

          const QString input = "2017-10-19 14:08:58,325 TRACE [Publisher] C.DataPublisher [DataPublisher.java:159] Publishing {Instrument12=-900.0, Instrument0=82.4, Instrument11=-900.0, Instrument1=131.16875000000005, Instrument2=1.0, Instrument13=0.0, Instrument3=120.091247064, Instrument6=-0.9, Instrument22, Instrument4=83.91875000000005, Instrument12Status=1.0, Instrument13Status=1.0}"
              "2017-10-19 14:08:58,825 TRACE [Publisher] C.DataPublisher [DataPublisher.java:159] Publishing {Instrument0=82.4, Instrument11=-900.0, Instrument1=135.33125000000007, Instrument3=120.091247064, Instrument4=83.91875000000005, Instrument22, Instrument23=-900.0, Instrument31=-900.0}";
              const QRegularExpression filterJunk(QStringLiteral("{(.+?)}"));
              const QRegularExpression instrumentRegExp(QStringLiteral(R"**(,?\s*Instrument(\d+)(?:Status)?\s*(?:=\s*([-+]?\d*\.?\d+(?:[eE][-+]?\d+)?))?)**"),QRegularExpression::CaseInsensitiveOption);
              auto i = filterJunk.globalMatch(input);
              while (i.hasNext()) {
                  auto j= instrumentRegExp.globalMatch(i.next().capturedRef(1));
                  while (j.hasNext()) {
                      const auto match = j.next();
                      QString result  = QStringLiteral("Instrument: %1").arg(match.capturedRef(1).toInt());
                      if(match.lastCapturedIndex()>1)
                      result  += QStringLiteral(" Value: %1").arg(match.capturedRef(2).toDouble());
                      qDebug() << result ;
                  }
              }
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          Joel BodenmannJ 1 Reply Last reply
          4
          • VRoninV VRonin

            If they are not nested (i.e. you never have {Instrument0=82.4,{Instrument11=-900.0, Instrument1=135.33125000000007}, Instrument4=83.91875000000005}) then you can use:

            const QString input = "2017-10-19 14:08:58,325 TRACE [Publisher] C.DataPublisher [DataPublisher.java:159] Publishing {Instrument12=-900.0, Instrument0=82.4, Instrument11=-900.0, Instrument1=131.16875000000005, Instrument2=1.0, Instrument13=0.0, Instrument3=120.091247064, Instrument6=-0.9, Instrument22, Instrument4=83.91875000000005, Instrument12Status=1.0, Instrument13Status=1.0}"
                "2017-10-19 14:08:58,825 TRACE [Publisher] C.DataPublisher [DataPublisher.java:159] Publishing {Instrument0=82.4, Instrument11=-900.0, Instrument1=135.33125000000007, Instrument3=120.091247064, Instrument4=83.91875000000005, Instrument22, Instrument23=-900.0, Instrument31=-900.0}";
                const QRegularExpression filterJunk(QStringLiteral("{(.+?)}"));
                const QRegularExpression instrumentRegExp(QStringLiteral(R"**(,?\s*Instrument(\d+)(?:Status)?\s*(?:=\s*([-+]?\d*\.?\d+(?:[eE][-+]?\d+)?))?)**"),QRegularExpression::CaseInsensitiveOption);
                auto i = filterJunk.globalMatch(input);
                while (i.hasNext()) {
                    auto j= instrumentRegExp.globalMatch(i.next().capturedRef(1));
                    while (j.hasNext()) {
                        const auto match = j.next();
                        QString result  = QStringLiteral("Instrument: %1").arg(match.capturedRef(1).toInt());
                        if(match.lastCapturedIndex()>1)
                        result  += QStringLiteral(" Value: %1").arg(match.capturedRef(2).toDouble());
                        qDebug() << result ;
                    }
                }
            
            Joel BodenmannJ Offline
            Joel BodenmannJ Offline
            Joel Bodenmann
            wrote on last edited by
            #8

            VRonin strikes again :D

            Industrial process automation software: https://simulton.com
            Embedded Graphics & GUI library: https://ugfx.io

            1 Reply Last reply
            1
            • M Offline
              M Offline
              MScottM
              wrote on last edited by
              #9

              For sure! Thanks VRonin.

              A couple more questions - is it possible to pass in a variable as part of the regex matching line?

              Also I don't seem to have the QRegularExpression Tool example (mentioned above). I'm using: Qt Creator 4.2.1 Based on Qt 5.8.0 (MSVC 2015, 32 bit)

              Do I need to update my IDE to get it?

              -Scott

              VRoninV 1 Reply Last reply
              0
              • M MScottM

                For sure! Thanks VRonin.

                A couple more questions - is it possible to pass in a variable as part of the regex matching line?

                Also I don't seem to have the QRegularExpression Tool example (mentioned above). I'm using: Qt Creator 4.2.1 Based on Qt 5.8.0 (MSVC 2015, 32 bit)

                Do I need to update my IDE to get it?

                -Scott

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #10

                is it possible to pass in a variable as part of the regex matching line?

                can you explain better? maybe with an example?

                Also I don't seem to have the QRegularExpression Tool example (mentioned above).

                I use https://regex101.com/ same principle, better colours and allows you to unit-test too

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  MScottM
                  wrote on last edited by
                  #11

                  Sorry, yes - I mean in the regular expression line:

                  instrumentRegExp(R"(,?\sInstrument(\d+)(?:Status)?\s(?:=\s*([-+]?\d*.?\d+(?:[eE][-+]?\d+)?))?)"

                  where its looking for an Instrument, is there a way to do something like this:

                  instrumentRegExp(R"(,?\sMyStringVariableHere(\d+)(?:Status)?\s(?:=\s*([-+]?\d*.?\d+(?:[eE][-+]?\d+)?))?)"

                  Regarding regular expression testing, I have realized that my text editor supports regex searching. I think it would be just a few tweaks to move them from there to QT.

                  Thanks again!

                  1 Reply Last reply
                  0
                  • Joel BodenmannJ Offline
                    Joel BodenmannJ Offline
                    Joel Bodenmann
                    wrote on last edited by Joel Bodenmann
                    #12

                    @MScottM said in QRegularExpressionMatch:

                    where its looking for an Instrument, is there a way to do something like this:
                    instrumentRegExp(R"(,?\sMyStringVariableHere(\d+)(?:Status)?\s(?:=\s*([-+]?\d*.?\d+(?:[eE][-+]?\d+)?))?)**"

                    Well, just insert them as a variable to a QString before you pass the QString to QRegularExpression().

                    QString myString("Static text with %1 some %2 variables").arg("foo").arg("bar");
                    

                    Industrial process automation software: https://simulton.com
                    Embedded Graphics & GUI library: https://ugfx.io

                    VRoninV 1 Reply Last reply
                    2
                    • M Offline
                      M Offline
                      MScottM
                      wrote on last edited by
                      #13

                      Dang...geniuses...all of you.

                      Thanks! Will be testing tonight.

                      1 Reply Last reply
                      0
                      • Joel BodenmannJ Joel Bodenmann

                        @MScottM said in QRegularExpressionMatch:

                        where its looking for an Instrument, is there a way to do something like this:
                        instrumentRegExp(R"(,?\sMyStringVariableHere(\d+)(?:Status)?\s(?:=\s*([-+]?\d*.?\d+(?:[eE][-+]?\d+)?))?)**"

                        Well, just insert them as a variable to a QString before you pass the QString to QRegularExpression().

                        QString myString("Static text with %1 some %2 variables").arg("foo").arg("bar");
                        
                        VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #14

                        Side note:

                        @Joel-Bodenmann Let's stare into the abyss of arg().arg(): http://doc.qt.io/qt-5/qstring.html#arg-12

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        Joel BodenmannJ kshegunovK 2 Replies Last reply
                        2
                        • VRoninV VRonin

                          Side note:

                          @Joel-Bodenmann Let's stare into the abyss of arg().arg(): http://doc.qt.io/qt-5/qstring.html#arg-12

                          Joel BodenmannJ Offline
                          Joel BodenmannJ Offline
                          Joel Bodenmann
                          wrote on last edited by
                          #15

                          @VRonin That was interesting - didn't know about that one :p

                          Industrial process automation software: https://simulton.com
                          Embedded Graphics & GUI library: https://ugfx.io

                          1 Reply Last reply
                          0
                          • VRoninV VRonin

                            Side note:

                            @Joel-Bodenmann Let's stare into the abyss of arg().arg(): http://doc.qt.io/qt-5/qstring.html#arg-12

                            kshegunovK Offline
                            kshegunovK Offline
                            kshegunov
                            Moderators
                            wrote on last edited by kshegunov
                            #16

                            You're a overplaying it a bit, aren't you? :)
                            I mean I knew of that pitfall, but there's nothing wrong in using arg().arg() as long as you're aware of this peculiarity, right?

                            Read and abide by the Qt Code of Conduct

                            VRoninV 1 Reply Last reply
                            0
                            • kshegunovK kshegunov

                              You're a overplaying it a bit, aren't you? :)
                              I mean I knew of that pitfall, but there's nothing wrong in using arg().arg() as long as you're aware of this peculiarity, right?

                              VRoninV Offline
                              VRoninV Offline
                              VRonin
                              wrote on last edited by VRonin
                              #17

                              Warning: philosophical talk below.

                              I'm actually surprised to hear this from you, @kshegunov. I used arg above so I'm not 100% dogmatic on it but lets take a good look at it:

                              arg is a search and replace so it's slower than QString::operator+ (if you concatenate a lot you can use operator%(const QString&,const QString&) in QStringBuilder). This should be reason enough not to use it. If you add the arg().arg() pitfall, that applies to any string you don't have complete control over all the aspect of input, it makes it almost evil.

                              The only reason to use arg() I can see is internationalisation, but, even then, you have to take care of just using arg(const QString&) as numbers shown to the user should be processed by QLocale and be mindful that if you call arg more than once on the same string you are opening yourself to a translator possibly introducing what looks like a logic bug in your code

                              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                              ~Napoleon Bonaparte

                              On a crusade to banish setIndexWidget() from the holy land of Qt

                              1 Reply Last reply
                              1
                              • M Offline
                                M Offline
                                MScottM
                                wrote on last edited by
                                #18

                                Success!!

                                This piece of code is working now! Nearly exactly as VRonin posted...Plus I learned something from the discussion above: I have a lot to learn :)

                                I wanted to ask about the capitol R that starts off the RegEx string - I figured out that it lets you use the single escape characters, but I couldn't find it documented anywhere...?

                                Thanks to all and best regards!

                                Scott

                                VRoninV 1 Reply Last reply
                                0
                                • M MScottM

                                  Success!!

                                  This piece of code is working now! Nearly exactly as VRonin posted...Plus I learned something from the discussion above: I have a lot to learn :)

                                  I wanted to ask about the capitol R that starts off the RegEx string - I figured out that it lets you use the single escape characters, but I couldn't find it documented anywhere...?

                                  Thanks to all and best regards!

                                  Scott

                                  VRoninV Offline
                                  VRoninV Offline
                                  VRonin
                                  wrote on last edited by
                                  #19

                                  @MScottM said in QRegularExpressionMatch:

                                  I wanted to ask about the capitol R that starts off the RegEx string - I figured out that it lets you use the single escape characters, but I couldn't find it documented anywhere...?

                                  https://en.wikipedia.org/wiki/C%2B%2B11#New_string_literals

                                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                  ~Napoleon Bonaparte

                                  On a crusade to banish setIndexWidget() from the holy land of Qt

                                  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