我需要编写正则表达式,以特殊符号捕获类型名称的通用参数(也可以是通用的),如下所示:
System.Action[Int32,Dictionary[Int32,Int32],Int32]
让我们假设类型名称是[\w.]+
,参数是[\w.,\[\]]+
,所以我只需要抓取Int32
,Dictionary[Int32,Int32]
并且Int32
基本上,如果平衡组堆栈为空,我需要采取一些措施,但我真的不明白如何。
UPD
下面的答案帮助我快速解决了问题(但没有适当的验证并且深度限制 = 1),但我已经设法通过组平衡来做到这一点:
^[\w.]+ #Type name
\[(?<delim>) #Opening bracet and first delimiter
[\w.]+ #Minimal content
(
[\w.]+
((?(open)|(?<param-delim>)),(?(open)|(?<delim>)))* #Cutting param if balanced before comma and placing delimiter
((?<open>\[))* #Counting [
((?<-open>\]))* #Counting ]
)*
(?(open)|(?<param-delim>))\] #Cutting last param if balanced
(?(open)(?!) #Checking balance
)$
UPD2(最后一次优化)
^[\w.]+
\[(?<delim>)
[\w.]+
(?:
(?:(?(open)|(?<param-delim>)),(?(open)|(?<delim>))[\w.]+)?
(?:(?<open>\[)[\w.]+)?
(?:(?<-open>\]))*
)*
(?(open)|(?<param-delim>))\]
(?(open)(?!)
)$