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 does not capture group correctly
Forum Updated to NodeBB v4.3 + New Features

QRegularExpression does not capture group correctly

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 979 Views 2 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.
  • LabanKL Offline
    LabanKL Offline
    LabanK
    wrote on last edited by
    #1

    Hi!
    I have a problem when trying to capture a part of a QString using QRegularExpression.
    I want to capture the part 12,0ak from the string, however only 12,0a was captured.

    QString str{"\t2,23 - 3,20\t12,0aK\t8713-9-7\t2140"};
    QRegularExpression rx{R"([\t:](\d{1,2},\d[aKMgL]*)[^\d%]\s)"};
    auto match = rx.match(str);
    qDebug() << match;
    

    QRegularExpressionMatch(Valid, has match: 0:(75, 83, "\t12,0aK\t"), 1:(76, 81, "12,0a"))

    When I try the regular expression at https://www.regextester.com/ it finds a match. So is this an error in QRegularExpression, or what I am doing wrong?
    Cheers!

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

      Hi,

      Neither https://regextester.com nor https://regex101.com matches anything with the pattern you gave and the test string.

      There might be an issue with the code you posted.

      If only the aK part interest you, you should rather use something like "\\\\t(\\d{1,2},\\d[aKMgL]+)\\\\t" that you can directly pass to the QRE constructor or the regex101 version: \\t(\d{1,2},\d[aKMgL]+)\\t.

      Note that if you want to have letters mandatory after the numbers you have to replace the * by a + otherwise these will be optional and the match might take something else.

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

        Hi!
        Thank you for taking your time giving an answer.
        Well, I tried the same pattern with QRegularExpression, QRegExp and std::regex and I get the same result for all of them, so I guess it is the pattern that is wrong.

        However, I still don't understand what I am doing wrong with the pattern, but I guess I will have to go back to the school bench...

        My task is to capture something like 12,0 or 12,0a or 12,0aK from a larger line read from file. Printing the line (a QString) gives:

        5,81%\t910 550\t10 466\t11,88\t15%\t2,23 - 3,20\t12,0aK\t8713-9-7\t2140 : 1
        

        This pattern works: R"([\t:](\d{1,2},\d[aKMgL]*)[^%\d])" (will capture 12,0aK)
        This pattern does not work: R"([\t:](\d{1,2},\d[aKMgL]*)[^%\d]\s)" (will only capture 12,0a)
        Note the trailing \s in the second example capturing a white space.

        I tried the second pattern at https://regex101.com/ and it captures 12,0a too. You need to type the pattern as: [\\t:](\d{1,2},\d[aKMgL]*)[^%\d]\\t in that case.

        So, nothing wrong with QRegularExpression, rather the pattern then.

        1 Reply Last reply
        0
        • LabanKL Offline
          LabanKL Offline
          LabanK
          wrote on last edited by
          #4

          And this is the pattern that actually works:

          QString patt{R"([\t:](\d{1,2},\d[aKMgL]*)[^%\d]?\s)"};
          

          Note the questionmark after the negating group.

          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