CSS 使用正则表达式在 C# 中突出显示语法
CSS syntax Highlighting in c# with regex
我想在我的 C# 富文本框中添加 CSS 语法突出显示。我将如何使用正则表达式来做到这一点,例如突出显示标签 names/classes/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);
您可以使用此模式来匹配 classes/ids:([\.#][_A-Za-z0-9\-]+)[^}]*{[^}]*}
。
我想在我的 C# 富文本框中添加 CSS 语法突出显示。我将如何使用正则表达式来做到这一点,例如突出显示标签 names/classes/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);
您可以使用此模式来匹配 classes/ids:([\.#][_A-Za-z0-9\-]+)[^}]*{[^}]*}
。