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

Mar*_*o M 4 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;
}
Run Code Online (Sandbox Code Playgroud)

我想从返回数组中排除单词列表,单词列表看起来像这样

string strWordsToExclude="if,you,me,about,more,but,by,can,could,did";
Run Code Online (Sandbox Code Playgroud)

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

Sel*_*enç 5

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

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