【问题标题】:Using Linq to filter out certain Keys from a Dictionary and return a new dictionary2使用 Linq 从字典中过滤掉某些键并返回一个新字典2
【发布时间】:2013-08-14 18:01:56
【问题描述】:

与本题相关:Using Linq to filter out certain Keys from a Dictionary and return a new dictionary

我有一个使用字典的自动完成控件。场景是我的 RichTextBox(用作代码编辑器)中的每个单词都会自动添加到我的自动完成列表中。就像我在 RichTextBox 中键入单词“asdasdasd”一样,单词“asdasdasd”将自动添加到我的自动完成中。

使用此代码:

private IEnumerable<AutocompleteItem> BuildList()
{
    //find all words of the text
    var words = new Dictionary<string, string>();

    var keysToBeFiltered = new HashSet<string> { "Do", "Not" };
    var filter = words.Where(p => !keysToBeFiltered.Contains(p.Key))
                      .ToDictionary(p => p.Key, p => p.Value);

    foreach (Match m in Regex.Matches(rtb_JS.Text, @"\b\w+\b"))
        filter[m.Value] = m.Value;

    //foreach (Match m in Regex.Matches(rtb_JS.Text, @"^(\w+)([=<>!:]+)(\w+)$"))
        //filter[m.Value] = m.Value;

    foreach (var word in filter.Keys)
    {
        yield return new AutocompleteItem(word);
    }
}

现在仍然包含“Do”和“Not”两个词,以便使用上面的代码自动完成。此外,当我的表单加载时,会出现一个特定的默认脚本,该脚本必须一直存在。所以我不能改变它。

我必须采取两种可能的解决方案来解决此问题: 1.不允许在默认脚本中使用的那些默认单词在表单加载时添加到我的自动完成中。(制作阻止添加到我列表中的单词列表) 2. 检测已注释“//”或“/*”的行并阻止其中的单词添加到我的字典中。

希望你能帮助我。请告诉我是否需要修改我的问题,我会尽快修改/更新。

main_Q:

如何防止将来自richtextbox(以//或/*开头的行)中的注释词添加到自动完成中

【问题讨论】:

  • 如何防止将richtextbox(以//或/*开头的行)中的注释词添加到自动完成中
  • 你的第二个正则表达式的原因是什么?
  • 我会补充一点,您的问题可能是错误的:// cmets 从那时起。 something // somethingelse 只有 somethingelse 被评论。 /* 是一样的,但是是多行的,所以在你得到 */ 之前,它仍然是评论的。
  • @xanatos 生病评论了第二个正则表达式对不起我的坏... 2. 那么 /* 和 */ 中的所有单词都不应该添加到 Richtextbox 中
  • @Elegiac 然后还有字符串...string foo = "// something"; // 不是评论。正确地做是一件痛苦的事。

标签: c# linq lambda


【解决方案1】:

我在以下行中发现了您的问题:

foreach (Match m in Regex.Matches(rtb_JS.Text, @"\b\w+\b"))
        filter[m.Value] = m.Value;

使用 "\b\w+\b" 正则表达式,您可以将 RichTextBox 控件中的所有单词添加到 filter 变量中。

因此,您必须更改该行中的代码以防止添加不需要的关键字。请检查以下内容:

private IEnumerable<AutocompleteItem> BuildList()
{
    //find all words of the text
    bool bolFindMatch = false;
    var words = new Dictionary<string, string>();

    var keysToBeFiltered = new HashSet<string> { "Do", "Not" };
    var filter = words.Where(p => !keysToBeFiltered.Contains(p.Key))
                      .ToDictionary(p => p.Key, p => p.Value);

    foreach (Match m in Regex.Matches(rtb1.Text, @"\b\w+\b"))
    {
        foreach (string hs in keysToBeFiltered)
        {
            if (Regex.Matches(m.Value, @"\b" + hs + @"\b").Count > 0)
            {
                bolFindMatch = true;
                break;
            }
        }

        if (!bolFindMatch)
        {
            filter[m.Value] = m.Value;
        }
        else
        {
            bolFindMatch = false;
        }
    }

    //foreach (Match m in Regex.Matches(rtb_JS.Text, @"^(\w+)([=<>!:]+)(\w+)$"))
        //filter[m.Value] = m.Value;

    foreach (var word in filter.Keys)
    {
        yield return new AutocompleteItem(word);
    }
}

【讨论】:

    【解决方案2】:

    为什么不在处理之前检查字符串以"//""/*" 开头的天气

        string notAllowed1 = @"//";
        string notAllowed2 = @"/*";
        var contains = false;
        foreach(string line in rtb_JS.Lines)
        {
           if (line.StartsWith(notAllowed2) || !line.StartsWith(notAllowed1))
           {
             contains = true;
             break
           }
       }
    //else do nothing
    

    使用 Linq 更新 2

        var contains = rtb_JS.Lines.ToList()
                      .Count( line => line.TrimStart().StartsWith(notAllowed2) || 
                       line.TrimStart().StartsWith(notAllowed1)) > 0 ;
    
    
    if(!contains)
    {
       //do your logic
    }
    

    【讨论】:

    • 我认为 .startswith 只表示文本的索引,而不是 perline 的索引
    • 那么 xanatos 的想法是完美的,进行修剪,然后将其拆分为多行并检查是否以 // 或 /* 开头的每一行天气
    • @xanatos 先生,您能帮帮我吗?
    • @Anand 字符串不包含“文本”的定义,并且找不到接受“字符串”类型的第一个参数的扩展方法“文本”
    【解决方案3】:

    我想这就是你想要的:

    var filter = Regex.Matches(rtb_JS.Text, @"\b\w+\b")
                      .OfType<Match>()
                      .Where(m=>!keysToBeFiltered.Any(x=>x == m.Value))
                      .ToDictionary(m=>m.Value,m=>m.Value);
    

    奇怪的是您的Dictionary 在一个条目中具有KeyValue 作为相同的值?

    【讨论】:

    • 已添加具有相同键的项目,指向该代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-26
    • 2022-12-07
    • 1970-01-01
    相关资源
    最近更新 更多