Reputation: 544
I'm trying to use QRegularExpression and it's been working fine for all my patterns exept this.
I have this text:
TYPE "ASDF"
some content
END_TYPE
TYPE "QWER"
some other content
END_TYPE
TYPE "ZXCV"
blablablabla
END_TYPE
and I want to match all TYPE...END_TYPE blocks (3 matches) in a QRegularExpression like this
QRegularExpression re("(TYPE(?:[^E]|E[^N]|EN[^D]|END[^_]|END_[^T])*END_TYPE)", QRegularExpression::DotMatchesEverythingOption);
QRegularExpressionMatchIterator i = re.globalMatch(text);
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
if (match.hasMatch()) {
QString captured = match.captured(0);
//.. and do some stuff with that string
}
}
but when I build and debug the code I get a Segmentation fault error window at the QRegularExpressionMatchIterator:
The inferior stopped because it received a signal from the Operating System. Signal Name: SIGSEGV, Signal meaning: Segmentation fault
I've also tried using negative look-arounds:
(TYPE ((?!END_TYPE))+END_TYPE)
This does not give me an error but also gives me no match.
Does any one have any idea what I'm doing wrong here or suggestion on how I can improve the regular expression? Any suggestions are greatly appreciated :)
Upvotes: 0
Views: 2050
Reputation: 544
Ok with great help from RyanJ and CAustin I solved my problem. this is how the code looks now:
QRegularExpression re("(TYPE .+END_TYPE)");
re.setPatternOptions(QRegularExpression::DotMatchesEverythingOption | QRegularExpression::InvertedGreedinessOption);
QRegularExpressionMatchIterator i = re.globalMatch(text);
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
if (match.hasMatch()) {
QString captured = match.captured(0);
}
}
It looks like QRegularExpression does not support negative look-arounds like (TYPE ((?!END_TYPE))+END_TYPE) or grouping like (TYPE(?:[^E]|E[^N]|EN[^D]|END[^]|END[^T])*END_TYPE)
So the fix is to use the QRegularExpression::InvertedGreedinessOption (works the same as QRegExp::setMinimal()) for minimal matching.
Thank you RyanJ and CAustin for your help!
Upvotes: 2