可以说我的输入是fn(a(b,c),d) fn(a,d) fn(a(b),d)
,我想a(b,c),d
如何编写一个模式来获取 () 中的所有内容?第二个 fn() 很容易第一个和第三个我不知道如何匹配
user34537
问问题
191 次
2 回答
5
为此,您需要平衡组定义:
result = Regex.Match(subject,
@"(?<=\() # Make sure there's a ( before the start of the match
(?> # now match...
[^()]+ # any characters except parens
| # or
\( (?<DEPTH>) # a (, increasing the depth counter
| # or
\) (?<-DEPTH>) # a ), decreasing the depth counter
)* # any number of times
(?(DEPTH)(?!)) # until the depth counter is zero again
(?=\)) # Make sure there's a ) after the end of the match",
RegexOptions.IgnorePatternWhitespace).Value;
于 2013-08-19T12:25:25.113 回答
2
你可以拆分它
var output=Regex.Split(input,@"(?:\)|^)[^()]*(?:\(|$)");
你会得到输出
a(b,()c),d
a,d
a(b),d
于 2013-08-19T12:28:15.643 回答