我的这些示例性类型的字符串:
I have these examplary type strings:
"System.Collections.Generic.IEnumerable`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"
"System.Collections.IEnumerable"
"System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Type, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"
"Whatever`3[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[ImaginaryType],[System.Type, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"
使用正则表达式,我想提取的主要类型,它的泛型类型统计,所有的泛型类型本身,所以上面的四个例子,我赶这些元素相应:
Using regex, I want to extract the main-type, its generic types count, and all the generic types themselves, so for the four examples above, I "catch" these elements correspondingly:
"System.Collections.Generic.IEnumerable"
1
"System.String"
"System.Collections.IEnumerable"
0
"System.Collections.Generic.Dictionary"
2
"System.Int32"
"System.Type"
"Whatever"
3
"System.Int32"
"ImaginaryType"
"System.Type"
有一个正则表达式可以做到这一点?
Is there a regex that can do that?
推荐答案
你可以用这种模式做到这一点:
you can do it with this pattern:
string pattern = @"
(?: # two possible entry points
G(?!A) # contigous to the precedent match
| # OR
A # at the start of the string
(?<main> [^`]+ ) ` (?<number> [0-9]+ ) [
)
[ (?<type> [^],]+ ) # generic type
[^]]* ] # all until the next closing square bracket
(?: , | ]z )
| A (?<main> [^`]+ ) # or a main-type without generic types
";
RegexOptions options = RegexOptions.IgnorePatternWhitespace;
foreach (Match match in Regex.Matches(input, pattern, options)) { ...
如果你的项目使用的模式几次,最好是一劳永逸编译。 请注意,您可以使用这个变量,而不是降低了正则表达式引擎的工作:
If you project to use the pattern several times, it's better to compile it once and for all. Note that you can reduce the regex engine work using this variant instead:
string pattern = @"
G(?!A) [
(?<type> [^],]+ )
[^]]* ] (?: , | ]z )
|
A
(?<main> [^`]+ )
(?:
` (?<number> [0-9]+ )
[{2}
(?<type> [^],]+ )
[^]]* ]
(?: , | ]z )
|
z
)";
如果你想确保字符串的结尾已经达到可以取代] Z
与(小于?endcheck&GT;] Z)
键,如果组存在于最后一场比赛的控制。
If you want to ensure that the end of the string has been reached you can replace ]z
with (?<endcheck>]z)
and control if the group exist in the last match.
相关推荐
最新文章