How to cut specified words from a string

There is a list of forbidden words (or lines that should be more general) and another list with which you can send messages to users. I would like to send all forbidden words from all letters.

Trivial example:

foreach(string word in wordsList)
{
   foreach(string mail in mailList)
   {
      mail.Replace(word,String.Empty);
   }
}

How can I improve this algorithm?


Thanks for the tips. I voted for a few answers, but I did not notice any answers, as this was more like a discussion than a solution. Some people have missed forbidden words with bad words. In my case, I don’t have to worry about recognizing β€œsh1t” or anything like that.

+5
source share
12 answers

RegEx, :

var bannedWords = @"\b(this|is|the|list|of|banned|words)\b";

foreach(mail in mailList)
    var clean = Regex.Replace(mail, bannedWords, "", RegexOptions.IgnoreCase);

, , , .

+2

. .

, "", ""? , - "a $$" - , ?

. ? .

+5
+2

(word1|word2|word3|...), , . , " ", (\b(word1|word2|word3|...)\b).

, , , , : , , .

+1

:

  • (.. )

, HashSet . Regex Replace, , .

HashSet<string> BannedWords = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase)
{
    "bad",
};

string Input = "this is some bad text.";

string Output = Regex.Replace(Input, @"\b\w+\b", (Match m) => BannedWords.Contains(m.Value) ? new string('x', m.Value.Length) : m.Value);
+1

* , , -, , . , , , "Grand ******* of Normandy", , , , , " "" , ( ), , .

, . 4chan, yahoo , medireview mediareview , eval ( , XSS, yahoo) (apparantly, medireview - !).

+1

: :

u SortedList, ur ( u ";" ), :

ur: Words: n item. ( O (1)). : K item. Z. Y, m = Z/Y.

ur O (n * K * Z).// knut

1. , u O (n log n).

2.1 - mailingListItem.Split( ";". ToCharArray()) : O (Z). 2.2 - : O (m * log m) O (K * Z) (m logm < Z).

3 - : O ((m + n) * k)

O ((m + n) * K + m * Z + n ^ 2) m < n, O (n ^ 2 + Z * K) , O (n * K * Z), n < K * Z (, ).

, , .

+1

Regex , . Regex , , . :

"\bBADWORD\b"

, mailList .

0

( ) , * - ? , , , , , .

0

, , , , clbuttic string.Replace(), . , , ( , , ). ... , , - .

, , " " . -,

/ -

0

, ( ) (, p [ass]). HashSet , , HashSet. , StringBuilder ( ).

0
0

All Articles