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. Trouble with QRegularExpression
Qt 6.11 is out! See what's new in the release blog

Trouble with QRegularExpression

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 601 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.
  • KroMignonK Offline
    KroMignonK Offline
    KroMignon
    wrote on last edited by KroMignon
    #1

    Hi all,

    I need a little parser for some JSON strings.
    Because my JSON string is quiet simple, I've tried to parse it with help of QRegularExpression.

    I first try to find Regex with help of Regex101.
    But this regex does not work with Qt.
    Can someone explain me what I am doing wrong?

    Hier the code extract:

    QString jsonStr("{\"priority\":0,\"sat\":17,\"event\":239,\"rssi\":2,\"io200\":0,\"ignition\":false,"
                    "\"battery\":9.934000000000001,\"hdop\":0.6000000000000001,\"power\":25.739,\"io24\":0,"
                    "\"operator\":26201,\"io219\":4051331153808338998,\"io220\":3906362710332356403,"
                    "\"io221\":3977299914081173504,\"distance\":0.0,\"totalDistance\":-1.231416135E7,\"motion\":false}");
    QRegularExpression jsonExtract("\"(\\w+)\":\"?([a-zA-Z0-9.\\-+]*)\"?");
    
    auto jsonMatches = jsonExtract.globalMatch(jsonStr);
    if(jsonMatches.hasNext())
    {
        auto m = jsonMatches.next();
        qDebug() << m;
    }
    

    EDIT
    The output is:

    QRegularExpressionMatch(Valid, has match: 0:(1, 13, ""priority":0"), 1:(2, 10, "priority"), 2:(12, 13, "0"))
    

    It must be obvious but I can see it :(

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

    jsulmJ 1 Reply Last reply
    0
    • KroMignonK KroMignon

      Hi all,

      I need a little parser for some JSON strings.
      Because my JSON string is quiet simple, I've tried to parse it with help of QRegularExpression.

      I first try to find Regex with help of Regex101.
      But this regex does not work with Qt.
      Can someone explain me what I am doing wrong?

      Hier the code extract:

      QString jsonStr("{\"priority\":0,\"sat\":17,\"event\":239,\"rssi\":2,\"io200\":0,\"ignition\":false,"
                      "\"battery\":9.934000000000001,\"hdop\":0.6000000000000001,\"power\":25.739,\"io24\":0,"
                      "\"operator\":26201,\"io219\":4051331153808338998,\"io220\":3906362710332356403,"
                      "\"io221\":3977299914081173504,\"distance\":0.0,\"totalDistance\":-1.231416135E7,\"motion\":false}");
      QRegularExpression jsonExtract("\"(\\w+)\":\"?([a-zA-Z0-9.\\-+]*)\"?");
      
      auto jsonMatches = jsonExtract.globalMatch(jsonStr);
      if(jsonMatches.hasNext())
      {
          auto m = jsonMatches.next();
          qDebug() << m;
      }
      

      EDIT
      The output is:

      QRegularExpressionMatch(Valid, has match: 0:(1, 13, ""priority":0"), 1:(2, 10, "priority"), 2:(12, 13, "0"))
      

      It must be obvious but I can see it :(

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @KroMignon said in Trouble with QRegularExpression:

      "?"

      Shouldn't it be ".*?" ?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

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

        I did not check your regex but maybe first switch to raw string literals to make sure you don't have a \ to much / missed somewhere?

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

        KroMignonK 1 Reply Last reply
        3
        • jsulmJ jsulm

          @KroMignon said in Trouble with QRegularExpression:

          "?"

          Shouldn't it be ".*?" ?

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

          @jsulm said in Trouble with QRegularExpression:

          Shouldn't it be ".*?" ?

          This was my first try, it did also not work.
          My problem is that I've only got 1 match (I've updated the first post to add the output)

          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
          • Christian EhrlicherC Christian Ehrlicher

            I did not check your regex but maybe first switch to raw string literals to make sure you don't have a \ to much / missed somewhere?

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

            @Christian-Ehrlicher said in Trouble with QRegularExpression:

            did not check your regex but maybe first switch to raw string literals to make sure you don't have a \ to much / missed somewhere?

            I don't think so.
            My problem is that I only got 1 match for the string.

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

            KroMignonK 1 Reply Last reply
            0
            • KroMignonK KroMignon

              @Christian-Ehrlicher said in Trouble with QRegularExpression:

              did not check your regex but maybe first switch to raw string literals to make sure you don't have a \ to much / missed somewhere?

              I don't think so.
              My problem is that I only got 1 match for the string.

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

              @Christian-Ehrlicher @jsulm Sorry for the moise... I AM BLIND!!
              I only had to replace if() by while()

              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