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. Getting error "called on an invalid QRegularExpression object"
QtWS25 Last Chance

Getting error "called on an invalid QRegularExpression object"

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 2.0k Views
  • 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
    lansing
    wrote on 19 Jun 2020, 08:44 last edited by
    #1

    I'm using QRegularExpression to catch text in my text file. The line of text goes like this :

    CHAPTER02NAME=my name
    CHAPTER03NAME=
    

    I want to match all the words after the = sign, including empty one. The regular expression I use is this

    QRegularExpression re("(?<=CHAPTER\d+NAME=).*");

    This works when I tested on a online regex tool, but when I use it on Qt, Qt Creator is gave me a warning "unknown escape sequence \d". And when I compiled and ran it in my program, I got the error :

    QRegularExpressionPrivate::doMatch(): called on an invalid QRegularExpression object

    What is wrong?

    J J 2 Replies Last reply 19 Jun 2020, 08:58
    0
    • L lansing
      19 Jun 2020, 08:44

      I'm using QRegularExpression to catch text in my text file. The line of text goes like this :

      CHAPTER02NAME=my name
      CHAPTER03NAME=
      

      I want to match all the words after the = sign, including empty one. The regular expression I use is this

      QRegularExpression re("(?<=CHAPTER\d+NAME=).*");

      This works when I tested on a online regex tool, but when I use it on Qt, Qt Creator is gave me a warning "unknown escape sequence \d". And when I compiled and ran it in my program, I got the error :

      QRegularExpressionPrivate::doMatch(): called on an invalid QRegularExpression object

      What is wrong?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 19 Jun 2020, 08:58 last edited by
      #2

      @lansing said in Getting error "called on an invalid QRegularExpression object":

      QRegularExpression re("(?<=CHAPTER\d+NAME=).*");

      Change to

      QRegularExpression re("(?<=CHAPTER\\d+NAME=).*");
      

      \ is an escape character in C++.

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

      1 Reply Last reply
      2
      • B Offline
        B Offline
        Bonnie
        wrote on 19 Jun 2020, 09:11 last edited by Bonnie
        #3

        Besides what @jsulm has said, the expression is still invalid even you escape the \ by using \\.
        You can check the reason by:

        if(!re.isValid())
            qDebug() << re.errorString();
        

        And the result is "lookbehind assertion is not fixed length".
        That means you cannot use "\d+". You must set a fixed length if you want to use lookbehind assertions.
        In this case : QRegularExpression re("(?<=CHAPTER\\d{2}NAME=).*")
        It seems to be a limitation of PCRE. So you maybe did not meet that when you tested in other languages.

        L 1 Reply Last reply 19 Jun 2020, 09:29
        1
        • B Bonnie
          19 Jun 2020, 09:11

          Besides what @jsulm has said, the expression is still invalid even you escape the \ by using \\.
          You can check the reason by:

          if(!re.isValid())
              qDebug() << re.errorString();
          

          And the result is "lookbehind assertion is not fixed length".
          That means you cannot use "\d+". You must set a fixed length if you want to use lookbehind assertions.
          In this case : QRegularExpression re("(?<=CHAPTER\\d{2}NAME=).*")
          It seems to be a limitation of PCRE. So you maybe did not meet that when you tested in other languages.

          L Offline
          L Offline
          lansing
          wrote on 19 Jun 2020, 09:29 last edited by
          #4

          @Bonnie

          Thanks this works, but I need it to match up to 3 times because the CHAPTER number can go up to three digits CHAPTER120NAME. I tried with this and it gives the "not fixed length" error again.

          QRegularExpression re("(?<=CHAPTER\\d{2,3}NAME=).*")

          B 1 Reply Last reply 19 Jun 2020, 09:31
          0
          • L lansing
            19 Jun 2020, 09:29

            @Bonnie

            Thanks this works, but I need it to match up to 3 times because the CHAPTER number can go up to three digits CHAPTER120NAME. I tried with this and it gives the "not fixed length" error again.

            QRegularExpression re("(?<=CHAPTER\\d{2,3}NAME=).*")

            B Offline
            B Offline
            Bonnie
            wrote on 19 Jun 2020, 09:31 last edited by
            #5

            @lansing
            Of course! {2,3} is not a fixed length, either...
            If you must use lookbehind assertions, I think you can only try several times to match different length of digits...

            1 Reply Last reply
            0
            • L lansing
              19 Jun 2020, 08:44

              I'm using QRegularExpression to catch text in my text file. The line of text goes like this :

              CHAPTER02NAME=my name
              CHAPTER03NAME=
              

              I want to match all the words after the = sign, including empty one. The regular expression I use is this

              QRegularExpression re("(?<=CHAPTER\d+NAME=).*");

              This works when I tested on a online regex tool, but when I use it on Qt, Qt Creator is gave me a warning "unknown escape sequence \d". And when I compiled and ran it in my program, I got the error :

              QRegularExpressionPrivate::doMatch(): called on an invalid QRegularExpression object

              What is wrong?

              J Offline
              J Offline
              JonB
              wrote on 19 Jun 2020, 09:33 last edited by JonB
              #6

              @lansing said in Getting error "called on an invalid QRegularExpression object":

              I want to match all the words after the = sign, including empty one.

              I don't understand why you are needing to use any kind of "lookbehind assertions" here at all?

              Something straightforward like:

              QRegularExpression re("CHAPTER\\d{2,3}NAME=(.*)")
              

              would seem to be all you are looking for, no?

              Or if you want the "chapter name" as well:

              QRegularExpression re("(CHAPTER\\d{2,3}NAME)=(.*)")
              

              ?

              L 1 Reply Last reply 19 Jun 2020, 09:39
              2
              • J JonB
                19 Jun 2020, 09:33

                @lansing said in Getting error "called on an invalid QRegularExpression object":

                I want to match all the words after the = sign, including empty one.

                I don't understand why you are needing to use any kind of "lookbehind assertions" here at all?

                Something straightforward like:

                QRegularExpression re("CHAPTER\\d{2,3}NAME=(.*)")
                

                would seem to be all you are looking for, no?

                Or if you want the "chapter name" as well:

                QRegularExpression re("(CHAPTER\\d{2,3}NAME)=(.*)")
                

                ?

                L Offline
                L Offline
                lansing
                wrote on 19 Jun 2020, 09:39 last edited by
                #7

                @JonB
                I don't need lookbehind, I thought this is lookahead referenced from a stackflow answer.

                J 1 Reply Last reply 19 Jun 2020, 09:41
                0
                • L lansing
                  19 Jun 2020, 09:39

                  @JonB
                  I don't need lookbehind, I thought this is lookahead referenced from a stackflow answer.

                  J Offline
                  J Offline
                  JonB
                  wrote on 19 Jun 2020, 09:41 last edited by JonB
                  #8

                  @lansing
                  OK, why do you want a "lookahead assertion" either? :) Put it like this: doesn't my first suggestion give what your question asks for, or the second one if you do want the chapter name? If they work it doesn't matter about whatever you were looking at on SO :)

                  L 1 Reply Last reply 19 Jun 2020, 09:52
                  0
                  • J JonB
                    19 Jun 2020, 09:41

                    @lansing
                    OK, why do you want a "lookahead assertion" either? :) Put it like this: doesn't my first suggestion give what your question asks for, or the second one if you do want the chapter name? If they work it doesn't matter about whatever you were looking at on SO :)

                    L Offline
                    L Offline
                    lansing
                    wrote on 19 Jun 2020, 09:52 last edited by
                    #9

                    @JonB

                    Thanks the first one works, I was able to capture it with match.captured(1)

                    QRegularExpressionMatch match= re.match(line);
                    
                    if (match.hasMatch()) {
                        title = match.captured(1);        
                    }
                    
                    J 1 Reply Last reply 19 Jun 2020, 10:01
                    0
                    • L lansing
                      19 Jun 2020, 09:52

                      @JonB

                      Thanks the first one works, I was able to capture it with match.captured(1)

                      QRegularExpressionMatch match= re.match(line);
                      
                      if (match.hasMatch()) {
                          title = match.captured(1);        
                      }
                      
                      J Offline
                      J Offline
                      JonB
                      wrote on 19 Jun 2020, 10:01 last edited by
                      #10

                      @lansing
                      Yep. For "most" cases, you can just just use (...) and pick out the desired capture(s). I don't know what you were looking at in SO, but suffice to say that the need for "lookahead" or "lookbehind" assertions is "unusual", unless you have a specific need.

                      1 Reply Last reply
                      0

                      1/10

                      19 Jun 2020, 08:44

                      • Login

                      • Login or register to search.
                      1 out of 10
                      • First post
                        1/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved