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

QRegularExpression Problem

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.3k 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.
  • C Offline
    C Offline
    cataylor559
    wrote on last edited by
    #1

    I am trying a simple pattern to extract numbers from a ASCII text file. The code I am using

    QTextStream in( &file );
    QString line = in.readline();
    QRegularExpression re;
    re.setPattern ( " -?[0-9]+\.?[0-9]* ");
    line.section(re, 0);

    a testing line was:
    "Expected Vibration Frequency = 2.52 MHz"

    Using RegexBuddy I tested the pattern and the results were
    2.52.

    However, when I implement the above code I get
    2.52 Mhz

    I tried the Qt expression of -?\d*\.?\d* and got the same thing. Why are the ascii characters after the digits included?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      welcome to devnet

      My guess is that the syntax handling used is different. You need to check the documentation of "QRegularExpression":http://qt-project.org/doc/qt-5/qregularexpression.html

      I have never used it. It has been implemented for Qt5. The older version "QRegExp":http://qt-project.org/doc/qt-5/QRegExp.html is also still available.There you have different possibilities.

      The "example code for QRegExp":http://qt-project.org/doc/qt-5/qtwidgets-tools-regexp-example.html is a handy tool to check different possibilities. Unfortunately, it is still based on the older QRegExp.

      Vote the answer(s) that helped you to solve your issue(s)

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

        Hi and welcome to devnet,

        I'd say you are using the wrong tool:

        @
        QString line("Expected Vibration Frequency = 2.52 MHz");
        QRegularExpression re;
        re.setPattern ( "-?[0-9]+\.?[0-9]*");
        QRegularExpressionMatch match = re.match(line);
        qDebug() << match.captured();
        @

        And you'll get 2.52

        Hope it helps

        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
        • C Offline
          C Offline
          cataylor559
          wrote on last edited by
          #4

          Thanks for the help. I had not tried to use the QRegularExpressionMatch. Changing code as your example showed solved the problem.

          Best,

          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