1

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?

tguen
  • 189
  • 1
  • 9
  • Could you give an example input, output you are getting and expected output for the regex? I just tested your `"(?m)a(.|\\r|\\n)*a"` expression and seems to work fine to me. – Eelke Dec 16 '18 at 09:40
  • 1
    Maybe `highlightBlock` only returns a single line? try printing the block to see what you are working on – Felix Dec 16 '18 at 09:55
  • 1
    as @Felix points highlightBlock returns the string of a QTextBlock, and a QTextBlock does not have line breaks. – eyllanesc Dec 16 '18 at 15:10

0 Answers0