fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. public class Test
  7. {
  8. public static void Main()
  9. {
  10. IEnumerable<string> titles = new string[]
  11. {
  12. "abc [FEAT one two] 123",
  13. "def(FEAT. three'four) 456",
  14. "ghi Featuring five",
  15. "jkl"
  16. };
  17.  
  18. var keys = new List<string> { "FEAT", "FEAT.", "Featuring" };
  19. keys = keys.OrderByDescending(x => x.Length).ToList();
  20. var pattern = $@"(?:{string.Join("|", keys.Select(z => Regex.Escape(z)))})\s*([^])]*)";
  21. Console.WriteLine(pattern);
  22.  
  23. var result = new List<string>();
  24. foreach(string title in titles)
  25. {
  26. var _ = Regex.Match(title, pattern);
  27. if (_.Success)
  28. result.Add(_.Groups[1].Value);
  29. }
  30.  
  31. Console.WriteLine( result.Count()); // Assert.AreEqual(3, result.Count());
  32. Console.WriteLine( result.Contains("one two") ); //Assert.IsTrue(result.Contains("one two"));
  33. Console.WriteLine( result.Contains("three'four") ); //Assert.IsTrue(result.Contains("three'four"));
  34. Console.WriteLine( result.Contains("five") ); // Assert.IsTrue(result.Contains("five"));
  35. }
  36. }
Success #stdin #stdout 0.08s 31200KB
stdin
Standard input is empty
stdout
(?:Featuring|FEAT\.|FEAT)\s*([^])]*)
3
True
True
True