简体   繁体   English

C#突出显示以冒号结尾的单词

[英]C# Highlight words that end with colon

I am using ScintillaNet, a wrapper for the Scintilla control in my C# application. 我正在使用ScintillaNet,它是C#应用程序中Scintilla控件的包装。 I am dynamically adding all words that end with : (let's just call this keyword from now). 我正在动态添加所有以:结尾的单词(从现在开始才调用此关键字)。 I did this by using a regex the string of keywords separated by a whitespace every time the user presses the colon key. 我通过使用正则表达式来完成此操作,每当用户按下冒号键时,关键字字符串就由空格分隔。 Here is my code (in the CharAdded event): 这是我的代码(在CharAdded事件中):

        if (e.Ch == ':')
        {
            string wp = string.Empty;
            Regex r = new Regex(@"\b\w+[:\b]");
            MatchCollection m = r.Matches(Scintilla.Text);
            for (int i = 0; i < m.Count; i++)
            {
                wp += " " + m[i].Value.Substring(0, m[i].Value.Length - 1); // Remove the colon
            }
            wp = wp.ToLower();
            Scintilla.Lexing.Keywords[3] = wp;
        }

Now the problem is, every time a new keyword is typed and the user presses the colon, instead of just highlighting the keyword, it will unnecessarily highlight every single keyword in the document again. 现在的问题是,每次键入一个新的关键字并用户按下冒号,而不是仅突出显示关键字,它都将不必要地再次突出显示文档中的每个关键字。 So although my coding works, it's pretty bad coding, and I am wondering how I can make my code faster by only highlighting the last keyword typed. 因此,尽管我的编码有效,但是这是非常糟糕的编码,我想知道如何仅突出显示键入的最后一个关键字来使我的代码更快。 Any help and/or ideas would be appreciated. 任何帮助和/或想法将不胜感激。

It is not unnecessary actually, You may only check for the last word but what if user pastes a long text? 实际上这不是不必要的,您可以只检查最后一个单词,但是如果用户粘贴长文本该怎么办? You really have to check every word one by one unless you are sure that the user won't paste a text into the control or edit the middle of the text.. 除非您确定用户不会将文本粘贴到控件中或编辑文本的中间,否则您确实必须一个一个地检查每个单词。

如果您的文本确实很长,并且您想改善处理时间,那么如何尝试插入一个隐藏标记以将单词标记为“突出显示”并在正则表达式中忽略它们呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM