fork download
  1. using System;
  2. using System.Linq;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace ConsoleApplication1
  6. {
  7. public class Program
  8. {
  9. public static void Main(string[] args)
  10. {
  11. var implicitParenRegexes = @"
  12. (a)(?(?=.) )(b) (c)
  13. (a)(?(?!z) )(b) (c)
  14. (a)(?(?<=.) )(b) (c)
  15. (a)(?(?<! ) )(b) (c)
  16. (a)(?(?: ) )(b) (c)
  17. (a)(?(?i:.) )(b) (c)
  18. (a)(?(?>.) )(b) (c)
  19. (a)(?(?(1).) )(b) (c)
  20. ((?<n>a))(?(?(n).) )(b)(c)
  21. (a)(?(?(?:.).) )(b) (c)
  22. (a)(?(?<n>.) )(b) (c)
  23. (a)(?(?'n'.) )(b) (c)
  24. (a)(?(?'-n' .) )(b) (c)
  25. (?<a>a)(?(?<a-n>.) )(b) (c)
  26. (a)(?(?# comment) )(b) (c)
  27. (a)(?(?i) )(b) (c)
  28. (a)(?(?(.).) )(b) (c)
  29. ".Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
  30. var explicitParenRegexes = @"
  31. (a)(?((?=.)) )(b) (c)
  32. (a)(?((?!z)) )(b) (c)
  33. (a)(?((?<=.)) )(b) (c)
  34. (a)(?((?<! )) )(b) (c)
  35. (a)(?((?: )) )(b) (c)
  36. (a)(?((?i:.)) )(b) (c)
  37. (a)(?((?>.)) )(b) (c)
  38. (a)(?((?(1).)) )(b) (c)
  39. ((?<n>a))(?((?(n).)) )(b)(c)
  40. (a)(?((?(?:.).)) )(b) (c)
  41. (a)(?((?<n>.)) )(b) (c)
  42. (a)(?((?'n'.)) )(b) (c)
  43. (a)(?((?'-n' .)) )(b) (c)
  44. (?<a>a)(?((?<a-n>.)) )(b) (c)
  45. (a)(?((?# comment)) )(b) (c)
  46. (a)(?((?i)) )(b) (c)
  47. (a)(?((?(.).)) )(b) (c)
  48. ".Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
  49.  
  50. Console.WriteLine("IMPLICIT PARENTHESIZED CONDITIONS:");
  51. foreach (var pattern in implicitParenRegexes)
  52. CheckPattern(pattern);
  53.  
  54. Console.WriteLine("\nEXPLICIT PARENTHESIZED CONDITIONS:");
  55. foreach (var pattern in explicitParenRegexes)
  56. CheckPattern(pattern);
  57.  
  58. Console.ReadKey();
  59. }
  60.  
  61. const string input = "a b c";
  62. static void CheckPattern(string pattern)
  63. {
  64. Console.Write("Regex: '{0}'; groups: ", pattern);
  65.  
  66. // Comment this block to get the exception
  67. if (pattern.Contains("(?(?i)"))
  68. {
  69. Console.WriteLine("Skipped to allow it run in ideone");
  70. return;
  71. }
  72.  
  73. try
  74. {
  75. int index = 1;
  76. Console.WriteLine(string.Join("; ",
  77. Regex.Match(input, pattern).Groups.Cast<Group>().Skip(1).Take(3)
  78. .Select(g => string.Format("${0}: '{1}'", index++, g.Value))));
  79. }
  80. catch (Exception exception)
  81. {
  82. Console.WriteLine("ERROR: {0}: {1}", exception.GetType().Name, exception.Message);
  83. }
  84. }
  85. }
  86. }
Success #stdin #stdout 0.04s 30280KB
stdin
Standard input is empty
stdout
IMPLICIT PARENTHESIZED CONDITIONS:
Regex: '(a)(?(?=.) )(b) (c)'; groups: $1: 'a'; $2: 'c'; $3: ''
Regex: '(a)(?(?!z) )(b) (c)'; groups: $1: 'a'; $2: 'c'; $3: ''
Regex: '(a)(?(?<=.) )(b) (c)'; groups: $1: 'a'; $2: 'c'; $3: ''
Regex: '(a)(?(?<! ) )(b) (c)'; groups: $1: 'a'; $2: 'c'; $3: ''
Regex: '(a)(?(?: ) )(b) (c)'; groups: $1: 'a'; $2: 'c'; $3: ''
Regex: '(a)(?(?i:.) )(b) (c)'; groups: $1: 'a'; $2: 'c'; $3: ''
Regex: '(a)(?(?>.) )(b) (c)'; groups: $1: 'a'; $2: 'c'; $3: ''
Regex: '(a)(?(?(1).) )(b) (c)'; groups: $1: 'a'; $2: 'c'; $3: ''
Regex: '((?<n>a))(?(?(n).) )(b)(c)'; groups: 
Regex: '(a)(?(?(?:.).) )(b) (c)'; groups: $1: 'a'; $2: 'c'; $3: ''
Regex: '(a)(?(?<n>.) )(b) (c)'; groups: ERROR: ArgumentException: parsing '(a)(?(?<n>.) )(b) (c)' - Alternation conditions do not capture and cannot be named.
Regex: '(a)(?(?'n'.) )(b) (c)'; groups: ERROR: ArgumentException: parsing '(a)(?(?'n'.) )(b) (c)' - Alternation conditions do not capture and cannot be named.
Regex: '(a)(?(?'-n' .) )(b) (c)'; groups: ERROR: ArgumentException: parsing '(a)(?(?'-n' .) )(b) (c)' - Alternation conditions do not capture and cannot be named.
Regex: '(?<a>a)(?(?<a-n>.) )(b) (c)'; groups: ERROR: ArgumentException: parsing '(?<a>a)(?(?<a-n>.) )(b) (c)' - Alternation conditions do not capture and cannot be named.
Regex: '(a)(?(?# comment) )(b) (c)'; groups: ERROR: ArgumentException: parsing '(a)(?(?# comment) )(b) (c)' - Alternation conditions cannot be comments.
Regex: '(a)(?(?i) )(b) (c)'; groups: Skipped to allow it run in ideone
Regex: '(a)(?(?(.).) )(b) (c)'; groups: $1: 'a'; $2: 'b'; $3: 'c'

EXPLICIT PARENTHESIZED CONDITIONS:
Regex: '(a)(?((?=.)) )(b) (c)'; groups: $1: 'a'; $2: 'b'; $3: 'c'
Regex: '(a)(?((?!z)) )(b) (c)'; groups: $1: 'a'; $2: 'b'; $3: 'c'
Regex: '(a)(?((?<=.)) )(b) (c)'; groups: $1: 'a'; $2: 'b'; $3: 'c'
Regex: '(a)(?((?<! )) )(b) (c)'; groups: $1: 'a'; $2: 'b'; $3: 'c'
Regex: '(a)(?((?: )) )(b) (c)'; groups: $1: 'a'; $2: 'b'; $3: 'c'
Regex: '(a)(?((?i:.)) )(b) (c)'; groups: $1: 'a'; $2: 'b'; $3: 'c'
Regex: '(a)(?((?>.)) )(b) (c)'; groups: $1: 'a'; $2: 'b'; $3: 'c'
Regex: '(a)(?((?(1).)) )(b) (c)'; groups: $1: 'a'; $2: 'b'; $3: 'c'
Regex: '((?<n>a))(?((?(n).)) )(b)(c)'; groups: 
Regex: '(a)(?((?(?:.).)) )(b) (c)'; groups: $1: 'a'; $2: 'c'; $3: ''
Regex: '(a)(?((?<n>.)) )(b) (c)'; groups: $1: 'a'; $2: 'b'; $3: 'c'
Regex: '(a)(?((?'n'.)) )(b) (c)'; groups: $1: 'a'; $2: 'b'; $3: 'c'
Regex: '(a)(?((?'-n' .)) )(b) (c)'; groups: ERROR: ArgumentException: parsing '(a)(?((?'-n' .)) )(b) (c)' - Reference to undefined group name n.
Regex: '(?<a>a)(?((?<a-n>.)) )(b) (c)'; groups: ERROR: ArgumentException: parsing '(?<a>a)(?((?<a-n>.)) )(b) (c)' - Reference to undefined group name n.
Regex: '(a)(?((?# comment)) )(b) (c)'; groups: $1: 'a'; $2: 'b'; $3: 'c'
Regex: '(a)(?((?i)) )(b) (c)'; groups: $1: 'a'; $2: 'b'; $3: 'c'
Regex: '(a)(?((?(.).)) )(b) (c)'; groups: $1: 'a'; $2: 'b'; $3: 'c'