Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Getting error "called on an invalid QRegularExpression object"



  • 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?


  • Qt Champions 2019

    @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++.



  • 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.



  • @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=).*")



  • @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...



  • @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)=(.*)")
    

    ?



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



  • @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 :)



  • @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);        
    }
    


  • @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.


Log in to reply