C# 如何分割特殊字符

C# 如何分割特殊字符,c#,C#,所以我有一个代码,我一直在改进它,使它尽可能地工作。现在它工作得很好,尽管我需要它来过滤整个句子,不管单词周围有什么特殊的字符。例如,当我发送字符串时: JOIN GooGle | × ,,. ¬ hiring !HOteL, it is ++ !!free!! ,, ..!community;;+_ 禁止使用的词语是加入、雇佣、酒店、免费、社区,它不会检测到上面的句子 我的代码是: public bool CheckSentence(string messageText.ToLower())

所以我有一个代码,我一直在改进它,使它尽可能地工作。现在它工作得很好,尽管我需要它来过滤整个句子,不管单词周围有什么特殊的字符。例如,当我发送字符串时:

JOIN GooGle | × ,,. ¬ hiring !HOteL, it is ++ !!free!! ,, ..!community;;+_
禁止使用的词语是加入、雇佣、酒店、免费、社区,它不会检测到上面的句子

我的代码是:

public bool CheckSentence(string messageText.ToLower())
{
    var count = 0;
    string[] wordsInMessage = messageText.Split(new char[] { ' ', ',' }, 
                                                StringSplitOptions.RemoveEmptyEntries);

    foreach (WordFilter Filter in this._filteredWords.ToList())
    {
        count += wordsInMessage.Count(x => x == Filter.Word);
    }

    return count >= 3;
}

如果我删除特殊字符,如
从单词中,它将起作用。我可以很容易地将这些字符添加到字符列表中,但肯定有一个非常简单的方法?

这可能会对您有所帮助,因为这取决于您对特殊字符的定义。我发现在大多数情况下,白名单而不是黑名单是最好的方法

现在剩下的字符串没有特殊字符,剩下的部分由代码完成

public bool CheckSentence(string messageText.ToLower())
{
    messageText = Regex.Replace(messageText, @"[^a-z0-9 ]", "");
    var count = 0;
    string[] wordsInMessage = messageText.Split(new char[] { ' ', ',' }, 
                                                StringSplitOptions.RemoveEmptyEntries);

    foreach (WordFilter Filter in this._filteredWords.ToList())
    {
        count += wordsInMessage.Count(x => x == Filter.Word);
    }

    return count >= 3;
}
string testData=@“加入谷歌|×,.,,,,,,招聘!酒店,它是++!!免费的!!,,,!社区;;+\ux”;
List BANDOWORDS=新列表
{
“加入”,
“雇用”,
“酒店”,
“免费”,
“社区”
};
bannedWords.ForEach(word=>
{
int startIndex=testData.IndexOf(word、StringComparison.InvariantCultureInogoreCase);
if(startIndex==-1)返回;
testData=testData.Remove(startIndex,word.Length);
});
Console.WriteLine(testData);

我使用了两个正则表达式,一个用于删除非字母字符的任何字符,另一个正则表达式用于删除字符串中的额外空格。然后我将原始字符串设置为小写,以匹配禁用单词列表。然后我简单地在空格上拆分字符串。希望这有帮助

static String input = "JOIN GooGle | × ,,. ¬ hiring !HOteL, it is ++ !!free!! ,, ..!community;;+_";
static Regex charOnly = new Regex("[^a-zA-Z ]");
static Regex extarSpaces = new Regex(@"\s{2,}");
static List<String> bannedWords = new List<String> { "join", "hiring", "hotel", "free", "community" };

static void Main(string[] args) {
  string originalString = charOnly.Replace(input, "");
  originalString = extarSpaces.Replace(originalString, " ");
  originalString = originalString.ToLower();
  string[] splitArray = originalString.Split(' ');
  int count = 0;
  for (int i = 0; i < splitArray.Length; i++) {
    if (splitArray[i] != null) {
      if (bannedWords.Contains(splitArray[i].ToString())) {
        count++;
        Console.WriteLine("Banned: " + splitArray[i].ToString());
      }
    }
  }
  Console.WriteLine("originalString: " + originalString);
  Console.WriteLine("splitArray Size: " + splitArray.Length);
  Console.WriteLine("Banned Words in string = " + count);
  Console.ReadKey();
}
static String input=“加入谷歌|×,.,,,,,,,,,,,招聘!酒店,它是++!!免费的!!,,…!社区;;++”;
静态正则表达式charOnly=新正则表达式(“[^a-zA-Z]”);
静态正则表达式extarSpaces=newregex(@“\s{2,}”);
静态列表bannedWords=新列表{“加入”、“租用”、“酒店”、“免费”、“社区”};
静态void Main(字符串[]参数){
字符串originalString=charOnly.Replace(输入“”);
originalString=extarSpaces.Replace(originalString,“”);
originalString=originalString.ToLower();
string[]splitArray=originalString.Split(“”);
整数计数=0;
for(int i=0;i
是的,亲爱的,让我们投票否决我的问题。为支持干杯。我没有投反对票,但你应该从你的代码应该做什么开始。“加入、招聘、酒店、免费、社区等被禁止的词语不会检测到上面的句子。”这不是一个明确的问题陈述。它不会被检测到,因为它周围有一些特殊的字符,例如!及;我想你应该做些过滤。为什么不使用
Contains()
?我将在什么时候添加此代码,它将如何过滤字符串?太棒了!如果我没有错的话,这会用一个空的空间来代替特殊字符吗?这将替换哪些字符,仅替换正则表达式中的字符?您已经在正则表达式中包含了
@“[^\w\s]”
,这将只过滤这些字符吗?因为当我使用时!及;它仍然没有检测到过滤过的单词。如果我用它来包装单词,它似乎不起作用!或或者,它仍然绕过它。绝对正确@AmmarSalman精彩的捕捉。:)Lemme updateFYI,您可能需要检查
startIndex
是否为-1<如果找不到字符串,code>IndexOf
将返回-1。如果在字符串中找不到禁止的单词。。。这会崩溃。在删除之前,我将添加一个检查,检查
startIndex
是否为>=0。
static String input = "JOIN GooGle | × ,,. ¬ hiring !HOteL, it is ++ !!free!! ,, ..!community;;+_";
static Regex charOnly = new Regex("[^a-zA-Z ]");
static Regex extarSpaces = new Regex(@"\s{2,}");
static List<String> bannedWords = new List<String> { "join", "hiring", "hotel", "free", "community" };

static void Main(string[] args) {
  string originalString = charOnly.Replace(input, "");
  originalString = extarSpaces.Replace(originalString, " ");
  originalString = originalString.ToLower();
  string[] splitArray = originalString.Split(' ');
  int count = 0;
  for (int i = 0; i < splitArray.Length; i++) {
    if (splitArray[i] != null) {
      if (bannedWords.Contains(splitArray[i].ToString())) {
        count++;
        Console.WriteLine("Banned: " + splitArray[i].ToString());
      }
    }
  }
  Console.WriteLine("originalString: " + originalString);
  Console.WriteLine("splitArray Size: " + splitArray.Length);
  Console.WriteLine("Banned Words in string = " + count);
  Console.ReadKey();
}