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. [ANOTHER QUESTION] Can anyone explain me QRegExp?

[ANOTHER QUESTION] Can anyone explain me QRegExp?

Scheduled Pinned Locked Moved General and Desktop
11 Posts 6 Posters 5.7k 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.
  • L Offline
    L Offline
    Leon
    wrote on last edited by
    #1

    Yes i have seen the documentation but it is kinda hard to understand.. I would like an explanation...
    Lets say we have

    @ QString leon="1\\\\\\\\n2\\\\\\\\n3\\\\\\\\ni want only the next line\\\\\\\\nfrom this line i want only --->Hello guys!<---\\\\\\\\nend here i dont want it";
    @

    how can i get the Hello guys! only?

    so the code that must be used is something like

    @ QRegExp regex("time=.* ");
    regex.setMinimal(true);

        QStringList list;
        int pos = 0;
    
        while ((pos = regex.indexIn(leon, pos)) != -1)
        {
            list << regex.cap(0).remove(QChar(' '),Qt::CaseInsensitive);
            pos += regex.matchedLength();
        }
        if(!list.count()){
            //error
            return;
        }
         QString current=list.at(0);
        qDebug(current.toLocal8Bit());
    

    @

    i don't get what should i put in line 1 in the parentheses so the qdebug show Hello guys!

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dmcr
      wrote on last edited by
      #2

      something like @"--->()<---*"@ i guess

      dmcr

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sidewinder
        wrote on last edited by
        #3

        Regular expressions are powerful but require some love :). There are many engines with similar but not exactly the same future sets. For example, Qt 4 doesn't support look behinds.

        Personally, I liked tutorial on "this":http://www.regular-expressions.info/tutorial.html page the most. For testing, there are many online RegEx testers like "this one":http://www.rubular.com/.

        Now, back to your problem. Due to lack of look behinds, only reasonable way to extract "Hello guys" string I can see are following two steps:
        @
        QString leon="1\\n2\\n3\\ni want only the next line\\nfrom this line i want only --->Hello guys!<---\\nend here i dont want it";
        QRegExp regex("--->.*(?=<---)");
        if(regex.indexIn(leon) != -1){
        QStringList list;
        QString str;
        list = regex.capturedTexts();
        foreach(str,list)
        qDebug() << str.remove("--->");
        }
        @

        It is very likely someone will post one step regex only solution soon.

        Since there are 2 steps anyway you could simply use QString::remove() to remove everything before and including ---> and after and including <---.
        I'm not sure if there are significant difference in speed between 2 removes, or regex + remove version, but you can always benchmark it :).

        "Never memorize what you can look up in books."
        Albert Einstein

        1 Reply Last reply
        0
        • L Offline
          L Offline
          Leon
          wrote on last edited by
          #4

          [quote author="sidewinder" date="1343376961"]Regular expressions are powerful but require some love :). There are many engines with similar but not exactly the same future sets. For example, Qt 4 doesn't support look behinds.

          Personally, I liked tutorial on "this":http://www.regular-expressions.info/tutorial.html page the most. For testing, there are many online RegEx testers like "this one":http://www.rubular.com/.

          Now, back to your problem. Due to lack of look behinds, only reasonable way to extract "Hello guys" string I can see are following two steps:
          @
          QString leon="1\\\\n2\\\\n3\\\\ni want only the next line\\\\nfrom this line i want only --->Hello guys!<---\\\\nend here i dont want it";
          QRegExp regex("--->.*(?=<---)");
          if(regex.indexIn(leon) != -1){
          QStringList list;
          QString str;
          list = regex.capturedTexts();
          foreach(str,list)
          qDebug() << str.remove("--->");
          }
          @

          It is very likely someone will post one step regex only solution soon.

          Since there are 2 steps anyway you could simply use QString::remove() to remove everything before and including ---> and after and including <---.
          I'm not sure if there are significant difference in speed between 2 removes, or regex + remove version, but you can always benchmark it :).[/quote]

          Thanks a lot! It works, but i would like an explanation for line 2.. what are
          @.*(?=@
          i quess they remove <-- but why not do something similar for the -->?

          1 Reply Last reply
          1
          • S Offline
            S Offline
            sidewinder
            wrote on last edited by
            #5

            Step by step.
            @"--->"@
            matches "--->" :)

            @.@
            matches any number (
            ) of any characters (.)

            @(?=)@
            is called look ahead. It means "check if there is pattern ahead, but don't include it in results".

            So
            @.*(?=<---)@
            checks if after any number of any characters <--- is present.

            Qt doesn't support look behinds (?<=), so unfortunately something like that doesn't work:
            @(?<=--->).*(?=<---)@

            Hopefully it will work in Qt5 :).

            "Never memorize what you can look up in books."
            Albert Einstein

            1 Reply Last reply
            0
            • L Offline
              L Offline
              Leon
              wrote on last edited by
              #6

              Ok got it thanks ! :)

              1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

                [quote author="sidewinder" date="1343388065"]Hopefully it will work in Qt5 :).[/quote]

                Yes, but not without code changes. QRegExp was entangled too much into Qt code, it had to be left in Qt5. To solve regexp issues, a new class was introduced - "QRegularExpression":http://doc-snapshot.qt-project.org/5.0/qregularexpression.html. It uses a third party regexp engine that supports everything you might need and is also much faster :D

                (Z(:^

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  Leon
                  wrote on last edited by
                  #8

                  What if i only want to show the first capture?
                  For example

                  @QString lool="1/n3/n5/n6/n7/3/n5/n9";
                  QRegExp reg2("3.*5");
                  if(reg2.indexIn(lool) != -1){
                  QStringList list;
                  QString strq;
                  list = reg2.capturedTexts();
                  foreach(strq,list)
                  qDebug(strq.toLocal8Bit());
                  }@

                  it will show
                  @3/n5/n6/n7/3/n5@

                  while i want from it to show
                  @3/n5@

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    Leon
                    wrote on last edited by
                    #9

                    Anyone? Sorry for 2nd post..

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      alexandros
                      wrote on last edited by
                      #10

                      I can say that I have quite the same problem here... I want only the first occurrence of the match!

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        raaghuu
                        wrote on last edited by
                        #11

                        You have to turn off the 'greedy' mode for this... see "here":http://qt-project.org/doc/qt-4.8/qregexp.html#setMinimal

                        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