我正在处理子类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);
}
现在,在使用此类的应用程序中,我有以下示例日志QString:
来自主机的新传入连接,使用IP地址192.168.1.4 港口56748
端口号的正则表达式是什么(字符串末尾的数字 ,其后面是 port - 区分大小写)< strong> AND 激动地 ONE 空白?
答案 0 :(得分:1)
\d+$
正则表达式将匹配字符串末尾的1位或更多位数,无论左手上下文如何。
您应该考虑使用捕获方法:"\\bport +(\\d+)$"
(并访问捕获#1)。这需要进行一些代码调整。
原因是QRegExp does not support a lookbehind:
支持零宽度正和零宽度负前瞻断言
(?=pattern)
和(?!pattern)
,其语法与Perl相同。 Perl的lookbehind断言,“独立”子表达式和条件表达式 不支持。
如果您只需要匹配ASCII水平空格,则可以使用[ \t]
替换空格(或者您可以尝试使用\s
\r
并\n
排除"[^\\S\r\n]"
- {{ 1}})。
答案 1 :(得分:0)
是否有理由不能使用port \d+$
之类的内容?