Search code examples
c++qtqregexp

QRegExp compile time warnings


I am working on some Qt app, whose main window consists of QPlainTextEdit subclassed log window for outputting events. I have three types of messages:

  1. Information message, which represents a QString that begins with [INFO] substring
  2. Warning message, which represents a QString that begins with [WARNING] substring
  3. Error message, which represents a QString that begins with [ERROR] substring

Now, what I want to achieve with QSyntaxHighlighter class is to change color of these messages according to their type (INFO type - Qt::DarkBlue color, WARNING type - Qt::DarkYellow color, ERROR type - Qt::DarkRed color) and here is code chunk, which should have done the task:

void UeLogWindowTextHighlighter::ueSetupRules()
{
    UeHighlightRule* ueRuleInfo=new UeHighlightRule(this);
    UeHighlightRule* ueRuleWarning=new UeHighlightRule(this);
    UeHighlightRule* ueRuleError=new UeHighlightRule(this);

    this->ueInfoStartExpression()->setPattern("^[INFO].\*");        // FIRST WARNING
    this->ueWarningStartExpression()->setPattern("^[WARNING].\*");  // SECOND WARNING
    this->ueErrorStartExpression()->setPattern("^[ERROR].\*");      // THIRD WARNING

    this->ueInfoExpressionCharFormat()->setForeground(Qt::darkGray);
    this->ueWarningExpressionCharFormat()->setForeground(Qt::darkYellow);
    this->ueErrorExpressionCharFormat()->setForeground(Qt::darkRed);

    ueRuleInfo->ueSetPattern(this->ueInfoStartExpression());
    ueRuleInfo->ueSetTextCharFormat(this->ueInfoExpressionCharFormat());

    this->ueHighlightRules()->append(ueRuleInfo);

    ueRuleWarning->ueSetPattern(this->ueWarningStartExpression());
    ueRuleWarning->ueSetTextCharFormat(this->ueWarningExpressionCharFormat());

    this->ueHighlightRules()->append(ueRuleWarning);

    ueRuleError->ueSetPattern(this->ueErrorStartExpression());
    ueRuleError->ueSetTextCharFormat(this->ueErrorExpressionCharFormat());

    this->ueHighlightRules()->append(ueRuleError);
}   // ueSetupRules

However, when I compile the project, I get following warnings:

../../../gui/uelogwindowtexthighlighter.cpp: In member function 'void UeLogWindowTextHighlighter::ueSetupRules()': ../../../gui/uelogwindowtexthighlighter.cpp:58:47: warning: unknown escape sequence: '\*' [enabled by default]
     this->ueInfoStartExpression()->setPattern("^[INFO].\*");
                                               ^ ../../../gui/uelogwindowtexthighlighter.cpp:59:50: warning: unknown escape sequence: '\*' [enabled by default]
     this->ueWarningStartExpression()->setPattern("^[WARNING].\*");
                                                  ^ ../../../gui/uelogwindowtexthighlighter.cpp:60:48: warning: unknown escape sequence: '\*' [enabled by default]
     this->ueErrorStartExpression()->setPattern("^[ERROR].\*");
                                                ^

and consequently the messages are not colored (that is my suspicion). What is wrong with my regular expressions? I was following this question and answer on SO.


Solution

  • Star (*) didn't have to be escaped. Remove the \ or if you need the \ it should be escaped and write double \ (\\).