fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6.  
  7. public class Test
  8. {
  9. public static void Main()
  10. {
  11. var text = "(Today is a blue day) (Today is a good day) (Today is a BAD day) (Today is a green day) (Today is a blue day)";
  12. var matches = Regex.Matches(text, @"\((?>([^()]*\bBAD\b)?)[^()]*\)(?(1)(?!))")
  13. .Cast<Match>()
  14. .Select(x => x.Value)
  15. .ToList();
  16. foreach (var m in matches)
  17. Console.WriteLine(m);
  18. }
  19. }
Success #stdin #stdout 0.08s 28256KB
stdin
Standard input is empty
stdout
(Today is a blue day)
(Today is a good day)
(Today is a green day)
(Today is a blue day)