I am working on a C # regular expression that can match nested constructs (in this case, brackets), as well as arbitrary operators (in this case the “|” character).
I started using push-down automata as described here .
What I still have:
String pattern = @" (?# line 01) \( (?# line 02) (?> (?# line 03) \( (?<DEPTH>) (?# line 04) | (?# line 05) \) (?<-DEPTH>) (?# line 06) | (?# line 07) .? (?# line 08) )* (?# line 09) (?(DEPTH)(?!)) (?# line 10) \) "; var source = "((Name1| Name2) Blah) | (Name3 ( Blah | Blah))"; var matches = Regex.Matches(source, pattern, RegexOptions.IgnorePatternWhitespace); matches.Dump();
Gets the following results:
Desired Results:
// ((Name1| Name2) Blah) // | // (Name3 ( Blah | Blah))
Note: there may or may not be any operators between groups. For example, a source might look like this: ((Name1 | Name2) Blah) (Name3 (Blah | Blah)) "
source share