Corresponding groups of nested parentheses using Regex and Pushdown-Automata

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:

 // ((Name1| Name2) Blah) // (Name3 ( Blah | Blah)) 

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)) "

+4
source share
1 answer

You can try the following: (just adding |\| to the end)

 \((?>\((?<DEPTH>)|\)(?<-DEPTH>)|.?)*(?(DEPTH)(?!))\)|\| 
+3
source

Source: https://habr.com/ru/post/1487164/


All Articles