C# Regex 原子左括号和右括号
C# Regex Atomic Opening and Closing Brackets
我有以下占位符,我正在尝试使用原子组成对捕获双开括号和闭括号:
{{(?>{{(?)|}}(<-DEPTH>)|[^{}])*}}(?(DEPTH)(?!))
I am {{name}} and I have {{scales}: red {{metallic_scales}: shiny and glistering}} scales}}
可以肯定地说,我并没有真正取得多大成功。
所以我想要这样的东西:
- {{姓名}}
- {{鳞片}:红色{{metallic_scales}:闪闪发亮}}鳞片}}
- {{metallic_scales}:闪闪发亮}}
您可以使用
(?= # Start of a positive lookahead to enable overlapping matches
(?<results> # The group storing the results
{{ # {{ (left-hand delimiter)
(?> # Start of an atomic group
(?!{{|}}).| # Any char not starting {{ or }} char sequence, or
{{(?<DEPTH>)| # {{ and a value pushed on to DEPTH group stack, or
}}(?<-DEPTH>) # }} and a value popped from the DEPTH group stack
)* # Zero or more repetitions of the atomic group patterns
}} # }} substring
(?(DEPTH)(?!)) # Conditional construct failing the match if DEPTH stack is not empty
) # End of the results group
) # End of the lookahead
参见regex demo。
C# 声明:
Regex reg = new Regex(@"
(?= # Start of a positive lookahead to enable overlapping matches
(?<results> # The group storing the results
{{ # {{ (left-hand delimiter)
(?> # Start of an atomic group
(?!{{|}}).| # Any char not starting {{ or }} char sequence, or
{{(?<DEPTH>)| # {{ and a value pushed on to DEPTH group stack, or
}}(?<-DEPTH>) # }} and a value popped from the DEPTH group stack
)* # Zero or more repetitions of the atomic group patterns
}} # }} substring
(?(DEPTH)(?!)) # Conditional construct failing the match if DEPTH stack is not empty
) # End of the results group
) # End of the lookahead",
RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline);
var results = reg.Matches(text).Cast<Match>().Select(x => x.Groups["results"].Value);
我有以下占位符,我正在尝试使用原子组成对捕获双开括号和闭括号:
{{(?>{{(?)|}}(<-DEPTH>)|[^{}])*}}(?(DEPTH)(?!))
I am {{name}} and I have {{scales}: red {{metallic_scales}: shiny and glistering}} scales}}
可以肯定地说,我并没有真正取得多大成功。
所以我想要这样的东西:
- {{姓名}}
- {{鳞片}:红色{{metallic_scales}:闪闪发亮}}鳞片}}
- {{metallic_scales}:闪闪发亮}}
您可以使用
(?= # Start of a positive lookahead to enable overlapping matches
(?<results> # The group storing the results
{{ # {{ (left-hand delimiter)
(?> # Start of an atomic group
(?!{{|}}).| # Any char not starting {{ or }} char sequence, or
{{(?<DEPTH>)| # {{ and a value pushed on to DEPTH group stack, or
}}(?<-DEPTH>) # }} and a value popped from the DEPTH group stack
)* # Zero or more repetitions of the atomic group patterns
}} # }} substring
(?(DEPTH)(?!)) # Conditional construct failing the match if DEPTH stack is not empty
) # End of the results group
) # End of the lookahead
参见regex demo。
C# 声明:
Regex reg = new Regex(@"
(?= # Start of a positive lookahead to enable overlapping matches
(?<results> # The group storing the results
{{ # {{ (left-hand delimiter)
(?> # Start of an atomic group
(?!{{|}}).| # Any char not starting {{ or }} char sequence, or
{{(?<DEPTH>)| # {{ and a value pushed on to DEPTH group stack, or
}}(?<-DEPTH>) # }} and a value popped from the DEPTH group stack
)* # Zero or more repetitions of the atomic group patterns
}} # }} substring
(?(DEPTH)(?!)) # Conditional construct failing the match if DEPTH stack is not empty
) # End of the results group
) # End of the lookahead",
RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline);
var results = reg.Matches(text).Cast<Match>().Select(x => x.Groups["results"].Value);