fork(11) download
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. public class Test
  5. {
  6. public static void Main(){
  7.  
  8. string[] tests = {
  9. // matching
  10. "2",
  11. "1 AND 2",
  12. "12 OR 234",
  13. "(1) AND (2)",
  14. "(((1)) AND (2))",
  15. "1 AND 2 AND 3",
  16. "1 AND (2 OR (3 AND 4))",
  17. "1 AND (2 OR 3) AND 4",
  18. " ( 1 AND ( 2 OR ( 3 AND 4 ) )",
  19. "((1 AND 7) OR 6) AND ((2 AND 5) OR (3 AND 4))",
  20. "(1)",
  21. "(((1)))",
  22.  
  23. // should fail
  24. "1 2",
  25. "1(2)",
  26. "(1)(2)",
  27. "AND",
  28. "1 AND",
  29. "(1 AND 2",
  30. "1 AND 2)",
  31. "1 (AND) 2",
  32. "(1 AND 2))",
  33. "(1) AND 2)",
  34. "(1)() AND (2)",
  35. "((1 AND 7) OR 6) AND (2 AND 5) OR (3 AND 4))",
  36. "((1 AND 7) OR 6) AND ((2 AND 5 OR (3 AND 4))",
  37. ""
  38. };
  39.  
  40. string re = @"(?x)^
  41. \s* (?: (?<open> \( ) \s* )*
  42. \d+
  43. \s* (?: (?<-open> \) ) \s* )*
  44. (?:
  45. \s+(?:AND|OR)\s+
  46. \s* (?: (?<open> \( ) \s* )*
  47. \d+
  48. \s* (?: (?<-open> \) ) \s* )*
  49. )*
  50. (?(open)(?!))
  51. \z";
  52.  
  53. foreach (string str in tests){
  54. Match match = Regex.Match(str, re);
  55. if (match.Success)
  56. Console.WriteLine("matched: '{0}'", str);
  57. else
  58. Console.WriteLine("failed: '{0}'", str);
  59. }
  60. }
  61. }
Success #stdin #stdout 0.08s 37256KB
stdin
stdout
matched: '2'
matched: '1 AND 2'
matched: '12 OR 234'
matched: '(1) AND (2)'
matched: '(((1)) AND (2))'
matched: '1 AND 2 AND 3'
matched: '1 AND (2 OR (3 AND 4))'
matched: '1 AND (2 OR 3) AND 4'
matched: ' ( 1    AND ( 2 OR  ( 3 AND    4 )  )'
matched: '((1 AND 7) OR 6) AND ((2 AND 5) OR (3 AND 4))'
matched: '(1)'
matched: '(((1)))'
failed:  '1 2'
failed:  '1(2)'
failed:  '(1)(2)'
failed:  'AND'
failed:  '1 AND'
failed:  '(1 AND 2'
failed:  '1 AND 2)'
failed:  '1 (AND) 2'
failed:  '(1 AND 2))'
failed:  '(1) AND 2)'
failed:  '(1)() AND (2)'
failed:  '((1 AND 7) OR 6) AND (2 AND 5) OR (3 AND 4))'
failed:  '((1 AND 7) OR 6) AND ((2 AND 5 OR (3 AND 4))'
failed:  ''