fork download
  1. using System;
  2. using System.Linq;
  3. using System.Text.RegularExpressions;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. char[] separators = { ' ', '.', ',', '!', '?', ':', ';', '(', ')', '\t' };
  10. string[] wordsToDelete = { "Hello", "Thanks", "kinda" };
  11. string SepPattern = new String(separators).Replace(@"\", @"\\").Replace("^", @"\^").Replace("-", @"\-").Replace("]", @"\]");
  12. var pattern = $@"\b(?:{string.Join("|", wordsToDelete.Select(Regex.Escape))})\b[{SepPattern}]*";
  13. Console.WriteLine(pattern);
  14. Regex rx = new Regex(pattern, RegexOptions.Compiled);
  15. DeleteWordsFromText("Hello, how are you?", rx);
  16. DeleteWordsFromText("Thanks, I am kinda. good.", rx);
  17. }
  18. public static void DeleteWordsFromText(string text, Regex p)
  19. {
  20. Console.WriteLine($"---- {text} ----");
  21. Console.WriteLine(p.Replace(text, ""));
  22. }
  23. }
Success #stdin #stdout 0.03s 134592KB
stdin
Standard input is empty
stdout
\b(?:Hello|Thanks|kinda)\b[ .,!?:;()	]*
---- Hello, how are you? ----
how are you?
---- Thanks, I am kinda. good. ----
I am good.