I am working on subclassed QSyntaxHighlighter:
UeSyntaxHiglighter::UeSyntaxHiglighter(QTextDocument* const parent)
: QSyntaxHighlighter(parent)
{
UeHighlightRule ruleInfo;
UeHighlightRule ruleWarning;
UeHighlightRule ruleError;
UeHighlightRule ruleIPv4Address;
UeHighlightRule rulePortNumber;
UeHighlightRule ruleProtocolCommand;
UeHighlightRule ruleProtocolCommandAcknowledge;
QTextCharFormat ruleInfoFormat;
QTextCharFormat ruleWarningFormat;
QTextCharFormat ruleErrorFormat;;
QTextCharFormat ruleIPv4AddressFormat;
QTextCharFormat ruleProtocolCommandFormat;
QTextCharFormat ruleProtocolCommandAcknowledgeFormat;
ruleInfoFormat.setForeground(Qt::darkBlue);
ruleInfoFormat.setFontWeight(QFont::ExtraLight);
ruleInfo.pattern=QRegExp("^\\[INFO\\].*");
ruleInfo.format=ruleInfoFormat;
ruleWarningFormat.setForeground(Qt::darkYellow);
ruleWarningFormat.setFontWeight(QFont::Normal);
ruleWarning.pattern=QRegExp("^\\[WARNING\\].*");
ruleWarning.format=ruleWarningFormat;
ruleErrorFormat.setForeground(Qt::darkRed);
ruleErrorFormat.setFontWeight(QFont::ExtraBold);
ruleError.pattern=QRegExp("^\\[ERROR\\].*");
ruleError.format=ruleErrorFormat;
ruleIPv4AddressFormat.setFontWeight(QFont::ExtraBold);
ruleIPv4Address.pattern=QRegExp("(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})");
ruleIPv4Address.format=ruleIPv4AddressFormat;
//BUG apply same coloring rule to port number as IP address
rulePortNumber.pattern=QRegExp("\\d+$");
rulePortNumber.format=ruleIPv4AddressFormat;
ruleProtocolCommandFormat.setFontCapitalization(QFont::AllUppercase);
ruleProtocolCommandFormat.setFontWeight(QFont::ExtraBold);
ruleProtocolCommandFormat.setForeground(Qt::darkGreen);
//BUG rule is not applied, i.e. text coloring is not active
ruleProtocolCommand.pattern=QRegExp("^CMD.\\*");
ruleProtocolCommand.format=ruleProtocolCommandFormat;
ruleProtocolCommandAcknowledgeFormat.setFontCapitalization(QFont::AllUppercase);
ruleProtocolCommandAcknowledgeFormat.setFontWeight(QFont::ExtraBold);
ruleProtocolCommandAcknowledgeFormat.setForeground(Qt::darkMagenta);
//BUG rule is not applied, i.e. text coloring is not active
ruleProtocolCommandAcknowledge.pattern=QRegExp("^ACK.\\*");
ruleProtocolCommandAcknowledge.format=ruleProtocolCommandAcknowledgeFormat;
m_ueHighlightRules.append(ruleInfo);
m_ueHighlightRules.append(ruleWarning);
m_ueHighlightRules.append(ruleError);
m_ueHighlightRules.append(ruleIPv4Address);
m_ueHighlightRules.append(rulePortNumber);
m_ueHighlightRules.append(ruleProtocolCommand);
m_ueHighlightRules.append(ruleProtocolCommandAcknowledge);
}
Now, in app which is using this class, I have following sample log QString:
New incoming connection from host with IP address 192.168.1.4 using port 56748
What is the regular expression for port number (number at the end of string, which is following word port - case sensitive) AND excatly ONE whitespace?
The \d+$
regex will match 1 or more digits at the end of the string, regardless of the left-hand context.
You should consider using a capturing approach: "\\bport +(\\d+)$"
(and access the capture #1). That will require some code adjustment though.
The reason is that QRegExp does not support a lookbehind:
Both zero-width positive and zero-width negative lookahead assertions
(?=pattern)
and(?!pattern)
are supported with the same syntax as Perl. Perl's lookbehind assertions, "independent" subexpressions and conditional expressions are not supported.
The space can be replaced with [ \t]
if you only need to match ASCII horizontal whitespace (or you may try an \s
with \r
and \n
excluded - "[^\\S\r\n]"
).