space 和数字之后的单词 "port" 的正则表达式

regexp for word "port" following space and number

我正在研究 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);
}

现在,在使用此 class 的应用程序中,我有以下示例日志 QString

New incoming connection from host with IP address 192.168.1.4 using port 56748

端口号的正则表达式是什么(字符串末尾的数字,跟在单词后面port - 区分大小写)AND excatly ONE whitespace?

您有什么理由不能使用 port \d+$ 之类的东西吗?

无论左侧上下文如何,\d+$ 正则表达式都将匹配字符串末尾的 1 个或多个数字。

您应该考虑使用 捕获 方法:"\bport +(\d+)$"(并访问捕获 #1)。不过,这将需要进行一些代码调整。

原因是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.

如果你只需要匹配 ASCII 水平白色space,space 可以替换为 [ \t](或者你可以尝试 \s\r\n 排除 - "[^\S\r\n]")。