using System; using System.Linq; using System.Text.RegularExpressions; namespace ConsoleApplication1 { public class Program { public static void Main(string[] args) { var implicitParenRegexes = @" (a)(?(?=.) )(b) (c) (a)(?(?!z) )(b) (c) (a)(?(?<=.) )(b) (c) (a)(?(?<! ) )(b) (c) (a)(?(?: ) )(b) (c) (a)(?(?i:.) )(b) (c) (a)(?(?>.) )(b) (c) (a)(?(?(1).) )(b) (c) ((?<n>a))(?(?(n).) )(b)(c) (a)(?(?(?:.).) )(b) (c) (a)(?(?<n>.) )(b) (c) (a)(?(?'n'.) )(b) (c) (a)(?(?'-n' .) )(b) (c) (?<a>a)(?(?<a-n>.) )(b) (c) (a)(?(?# comment) )(b) (c) (a)(?(?i) )(b) (c) (a)(?(?(.).) )(b) (c) ".Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); var explicitParenRegexes = @" (a)(?((?=.)) )(b) (c) (a)(?((?!z)) )(b) (c) (a)(?((?<=.)) )(b) (c) (a)(?((?<! )) )(b) (c) (a)(?((?: )) )(b) (c) (a)(?((?i:.)) )(b) (c) (a)(?((?>.)) )(b) (c) (a)(?((?(1).)) )(b) (c) ((?<n>a))(?((?(n).)) )(b)(c) (a)(?((?(?:.).)) )(b) (c) (a)(?((?<n>.)) )(b) (c) (a)(?((?'n'.)) )(b) (c) (a)(?((?'-n' .)) )(b) (c) (?<a>a)(?((?<a-n>.)) )(b) (c) (a)(?((?# comment)) )(b) (c) (a)(?((?i)) )(b) (c) (a)(?((?(.).)) )(b) (c) ".Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); Console.WriteLine("IMPLICIT PARENTHESIZED CONDITIONS:"); foreach (var pattern in implicitParenRegexes) CheckPattern(pattern); Console.WriteLine("\nEXPLICIT PARENTHESIZED CONDITIONS:"); foreach (var pattern in explicitParenRegexes) CheckPattern(pattern); Console.ReadKey(); } const string input = "a b c"; static void CheckPattern(string pattern) { Console.Write("Regex: '{0}'; groups: ", pattern); // Comment this block to get the exception if (pattern.Contains("(?(?i)")) { Console.WriteLine("Skipped to allow it run in ideone"); return; } try { int index = 1; Console.WriteLine(string.Join("; ", Regex.Match(input, pattern).Groups.Cast<Group>().Skip(1).Take(3) .Select(g => string.Format("${0}: '{1}'", index++, g.Value)))); } catch (Exception exception) { Console.WriteLine("ERROR: {0}: {1}", exception.GetType().Name, exception.Message); } } } }
Standard input is empty
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'