gpt4 book ai didi

.net - 正则表达式跟踪 `)`

转载 作者:行者123 更新时间:2023-12-04 18:09:27 28 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Using RegEx to balance match parenthesis

(4 个回答)



Matching Nested Structures With Regular Expressions in Python

(6 个回答)


7年前关闭。




假设我的输入是 fn(a(b,c),d) fn(a,d) fn(a(b),d)我想要 a(b,c),d我将如何编写一个模式来获取 () 中的所有内容?第二个 fn() 很容易,第一个和第三个我不知道如何匹配

最佳答案

您需要 balancing group definitions为了这:

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;

关于.net - 正则表达式跟踪 `)`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18313660/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com