C# C正则表达式原子开括号和闭括号
我有一个占位符,我试图使用原子组来捕捉成对的双开始括号和结束括号: {{?>{{{}}{{{}}{{{}]*}}深度 我是{名字}我有{鳞片}:红色{金属鳞片}:闪亮的鳞片} 可以肯定地说,我并没有取得多大的成功 所以我想要的是: {{name}} {鳞片}:红色{金属鳞片}:闪亮的鳞片} {{金属鳞片}:闪闪发光} 你可以用C# C正则表达式原子开括号和闭括号,c#,regex,regex-group,C#,Regex,Regex Group,我有一个占位符,我试图使用原子组来捕捉成对的双开始括号和结束括号: {{?>{{{}}{{{}}{{{}]*}}深度 我是{名字}我有{鳞片}:红色{金属鳞片}:闪亮的鳞片} 可以肯定地说,我并没有取得多大的成功 所以我想要的是: {{name}} {鳞片}:红色{金属鳞片}:闪亮的鳞片} {{金属鳞片}:闪闪发光} 你可以用 (?= # Start of a positive lookahead to enable overlapping matches
(?= # 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
看
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);
你可以用
(?= # 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
看
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);
我建议您改变方法,使用堆栈而不是Regex什么是堆栈?检查一下,我还建议您搜索平衡括号实现perhaps,但如果可能的话,我希望使用Regex。我会研究你提供的东西,这似乎是一个好方法。括号中的开始和结束双精度用于确定分组何时真正结束。因为你可以做{{金属刻度}:闪亮的{彩色刻度}:单词}。我建议改变你的方法,使用堆栈而不是Regex什么是堆栈?检查一下,我也建议你搜索平衡括号实现sperhaps,但如果可能的话我想使用Regex。我会研究你提供的东西,这似乎是一个好方法。括号中的开始和结束双精度用于确定分组何时真正结束。因为你能做{金属音阶}:闪亮的{彩色音阶}:文字}。@MathiasChartier很高兴它对你有用。请考虑一下投票。@ MathiasChartier很高兴为你效劳。请考虑投票。