fork download
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Collections.Generic;
  4. using System.Collections;
  5. using System.Linq;
  6.  
  7. public class Test
  8. {
  9. public static void Main()
  10. {
  11. var phrases = new List<string> { @"(one)", @".two.", "[three]" };
  12. phrases = phrases.OrderByDescending(x => x.Length).ToList();
  13. var pattern = $@"(?!\B\w)(?:{string.Join("|", phrases.Select(z => Regex.Escape(z)))})(?<!\w\B)";
  14. var text = "[three] (one) .two. One[three]more time(one)here we.two.have a text";
  15. var matches = Regex.Matches(text, pattern).Cast<Match>().Select(x => x.Value).ToList();
  16. Console.WriteLine(string.Join(", ", matches));
  17. }
  18. }
Success #stdin #stdout 0.09s 30676KB
stdin
Standard input is empty
stdout
[three], (one), .two., [three], (one), .two.