【问题标题】:CSS syntax Highlighting in c# with regex使用正则表达式在 c# 中突出显示 CSS 语法
【发布时间】:2017-02-15 22:25:06
【问题描述】:

我想在 C# 中的富文本框中添加 CSS 语法突出显示。我将如何使用正则表达式来做到这一点,例如突出显示标签名称/类/ID。到目前为止,我已经为 HTM 做了这个,但我也想为 CSS 做。

            string tags = @"<([/A-Za-z0-9]*)\b[^>]*>(.*?)";
            tagMatches = Regex.Matches(rtb.Text, tags, RegexOptions.Multiline);

            // getting attributes from the text 
            string attributes = @"[A-Za-z0-9-_]*=[A-Za-z0-9-_]*";
            attributeMatches = Regex.Matches(rtb.Text, attributes);

            // getting comments (inline or multiline)
            string comments = @"(\<![ \r\n\t]*(--([^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\>)";
            commentMatches = Regex.Matches(rtb.Text, comments, RegexOptions.Multiline);

            // getting strings
            string strings = "(\".+?\"|\'.+?\')";
            stringMatches = Regex.Matches(rtb.Text, strings);

【问题讨论】:

    标签: c# css syntax-highlighting


    【解决方案1】:

    您可以使用此模式来匹配类/ID:([\.#][_A-Za-z0-9\-]+)[^}]*{[^}]*}

    Example

    【讨论】:

    • ([\.#_A-Za-z0-9\-][_A-Za-z0-9\-]+)[^}]*{[^}]*} 这样会更好
    • 我将如何处理 CSS 属性?我已经为此工作了一段时间,但一无所获。谢谢你能帮忙
    • 别担心,我已经为 @Zroq 这里 string attributes = @"([A-Za-z0-9-]*:)"; 计算了属性
    猜你喜欢
    • 1970-01-01
    • 2017-03-11
    • 1970-01-01
    • 1970-01-01
    • 2014-12-12
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多