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. [SOLVED]Weird regex behavior...
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]Weird regex behavior...

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.9k 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.
  • W Offline
    W Offline
    wecing
    wrote on last edited by
    #1

    Hi guys,

    I have some problem with regex in Qt. I wrote a small program to match all consecutive numbers in a given string, but the program fails to work correctly.

    Here is the code:
    @#include <QtGui>
    #include <iostream>

    int main() {
    QRegExp rx("[0-9]+");
    QString str = "35.625(+2.125)";
    int pos = 0;

     while ((pos = rx.indexIn(str, pos)) != -1) {
          std::cout << rx.cap(1).toStdString() << " ";
          pos += rx.matchedLength();
     }
     
     std::cout << std::endl;
     return 0;
    

    }
    @

    And the output is... an empty line?????

    1 Reply Last reply
    0
    • W Offline
      W Offline
      wecing
      wrote on last edited by
      #2

      Well... In fact I want to match all numbers; I added some redundant parenthesis, and "(([0-9]*)\.?([0-9]+))" works perfectly for me.

      Why? According to the doc --
      bq. For historical reasons, quantifiers (e.g. ) that apply to capturing parentheses are more "greedy" than other quantifiers. For example, a(a)* will match "aaa" with cap(1) == "aaa". This behavior is different from what other regexp engines do (notably, Perl). To obtain a more intuitive capturing behavior, specify QRegExp::RegExp2 to the QRegExp constructor or call setPatternSyntax(QRegExp::RegExp2).

      Parenthesis may cause problems; but I did use QRegExp::RegExp2, but it didn't help.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lgeyer
        wrote on last edited by
        #3

        You didn't specify any subexpressions (or capture groups or whatever you might call them) so the desired result is at rx.cap(0), not rx.cap(1).

        @
        ...
        std::cout << rx.cap(0).toStdString() << " ";
        ...

        35 625 2 125
        @

        EDIT Looks like you've already solved your initial problem.

        1 Reply Last reply
        0
        • W Offline
          W Offline
          wecing
          wrote on last edited by
          #4

          Ahh... Got it. Thanks.

          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