我有以下占位符,我试图使用原子组成对捕获双开括号和尾括号:
{{(?>{{((?)\{}}(<-纵深>)\{}^}*}]((深度)(?)) 我是{名字},我有{{刻度}:红色{{metallic_scales}:闪闪发光}}
可以肯定地说,我并没有取得多大的成功。
所以我想要这样的东西:
发布于 2019-11-24 12:09:06
你可以用
(?= # 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演示。
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);https://stackoverflow.com/questions/59014693
复制相似问题