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

QRegExp floating point matching

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

    i want to use foating points in a line and i tried to do that

            if(line.contains("lineto"))
            {
                QRegExp rxValues("\\d+\\.\\d+");    // primitive floating point matching
                rxValues.indexIn(line);
    
                out << "obj.lineTo(" + rxValues.cap(1) +  ", " + rxValues.cap(2) << "\n";
    
                qDebug() << rxValues.cap(1);
                qDebug() << line;
            }
    

    debug is like that

    ""
    "512.00 512.00 lineto"
    ""
    "512.00 0.00 lineto"
    ""
    "0.00 0.00 lineto"
    ""
    "0.00 512.00 lineto"

    why it doest work? why rxValues.cap(1) is empty?

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

      Hi,

      Because you're not capturing anything with that regexp. It should be "(\\d+)\\.(\\d+)"

      On a side note, if you are using Qt 5, consider moving to QRegularExpression.

      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
      • kybernetesK Offline
        kybernetesK Offline
        kybernetes
        wrote on last edited by kybernetes
        #3

        thanks for your fast reply.

        i tried your suggestion and it works like that

        qDebug() << rxValues.cap(1);
        qDebug() << rxValues.cap(2);
        qDebug() << line;

        "512"
        "00"
        "512.00 511.00 lineto"

        but i want the number "512.00" and "511.00"

        as u see cap(1) = 512 and cap(2) = 00

        and i was waiting cap(3) = 511 but i was "" nothing.

        1 Reply Last reply
        0
        • jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4
          QRegularExpression reg("(\\d+\\.\\d+)");
          qDebug() << reg.match("512.00 512.00 lineto");
          

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

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

            Sorry, I misunderstood your original problem, @jsulm suggestion is the best way to go.

            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
            • kybernetesK Offline
              kybernetesK Offline
              kybernetes
              wrote on last edited by kybernetes
              #6
                     Thanks for your answers. 
              

              i used QRegularExpression and write my code like below. i think captured(1) is second number for me but debug is like that captured(0) and captured(1) is same but i didnt why.

              i think;
              captured(0) has to be 512.00 and captured(1) has to be 0.00

                          QString line = "512.00 0.00 lineto";
                          QRegularExpression rxValues("(\\d+\\.\\d+)");
                          QRegularExpressionMatch match = rxValues.match(line);
              
                          qDebug()<< match.captured(0);
                          qDebug()<< match.captured(1);
                          qDebug()<< line;
              

              debug output

              "512.00"
              "512.00"
              "512.00 0.00 lineto"

              1 Reply Last reply
              0
              • jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7
                QRegularExpressionMatchIterator iter = reg.globalMatch("512.00 2.00 lineto");
                while (iter.hasNext()) {
                    qDebug() << iter.next().captured();
                }
                

                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