Getting error "called on an invalid QRegularExpression object"
-
wrote on 19 Jun 2020, 08:44 last edited by
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 thisQRegularExpression 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?
-
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 thisQRegularExpression 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?
@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++.
-
wrote on 19 Jun 2020, 09:11 last edited by Bonnie
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. -
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.wrote on 19 Jun 2020, 09:29 last edited byThanks 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=).*")
-
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=).*")
-
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 thisQRegularExpression 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?
wrote on 19 Jun 2020, 09:33 last edited by JonB@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)=(.*)")
?
-
@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.wrote on 19 Jun 2020, 09:41 last edited by JonB@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 :) -
@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 :) -
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); }
wrote on 19 Jun 2020, 10:01 last edited by@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/10