c# - 从文本中提取关键字并排除单词

标签 c# regex arrays string

我有这个功能可以从文本中提取所有单词

public static string[] GetSearchWords(string text)
{

    string pattern = @"\S+";
    Regex re = new Regex(pattern);

    MatchCollection matches = re.Matches(text);
    string[] words = new string[matches.Count];
    for (int i=0; i<matches.Count; i++)
    {
        words[i] = matches[i].Value;
    }
    return words;
}

我想从返回数组中排除一个单词列表,单词列表如下所示

string strWordsToExclude="if,you,me,about,more,but,by,can,could,did";

如何修改上述函数以避免返回列表中的单词。

最佳答案

string strWordsToExclude="if,you,me,about,more,but,by,can,could,did";
var ignoredWords = strWordsToExclude.Split(',');
return words.Except(ignoredWords).ToArray();

我认为Except方法适合您的需求

关于c# - 从文本中提取关键字并排除单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21691751/

相关文章:

regex - 从 R 中的字符串中提取日期

regex - awk regex 最后一次出现的字符/模式

java - 为什么未填充此数组?

c# - 锯齿状到多维数组

c# - 在唯一列表c#中合并嵌套列表的元素

javascript - RegEx 和 javascript 替换不匹配的字符串

Java - 返回数组中的多个字符串

arrays - D:创建模板化对象数组

c# - Servicestack 期望较低的程序集版本

c# - 使用 selenium 和 CssSelector 访问 html 标签