Qt 5.12.0
I'm using a class derived from QSyntaxHighlighter to highlight text in a QTextEdit widget. I've overridden the highlightBlock(const QString & text)
function and I'm trying to make a multi-line regex pattern using QRegularExpression.
QTextCharFormat format;
format.setForeground(QColor(0xFF, 0x00, 0x00));
auto opt = QRegularExpression::MultilineOption;
auto regex = QRegularExpression("a(.|\\n)*a", opt);
QRegularExpressionMatchIterator it = regex.globalMatch(text);
while (it.hasNext())
{
QRegularExpressionMatch match = it.next();
setFormat(match.capturedStart(), match.capturedLength(), format);
}
This matches any string that starts and ends with the letter a
, but only in a single line. I've tried several variants including:
auto opt = QRegularExpression::DotMatchesEverythingOption | QRegularExpression::MultilineOption;
auto regex = QRegularExpression("a.*a", opt);
...
auto opt = QRegularExpression::MultilineOption;
auto regex = QRegularExpression("(?m)a(.|\\r|\\n)*a", opt);
...
auto opt = QRegularExpression::NoPatternOption;
auto regex = QRegularExpression("(?m)a(.|\\r|\\n)*a", opt);
None of these work. Is multi-line regex just broken in Qt, or am I doing something wrong?