Regex.Replace("blahblah[122][122][122]blahblahblahblah[18][18][18][18]blahblahblah",
@"(\[([^]]+)])(\1)+",
m => "[" + m.Groups[2].Value + "," + (m.Groups[3].Captures.Count + 1) + "]")
戻り値:
blahblah[122,3]blahblahblahblah[18,4]blahblahblah
正規表現の説明:
( Starts group 1
\[ Matches [
( Starts group 2
[^]]+ Matches 1 or more of anything but ]
) Ends group 2
] Matches ]
) Ends group 1
( Starts group 3
\1 Matches whatever was in group 1
) Ends group 3
+ Matches one or more of group 3
ラムダの説明:
m => Accepts a Match object
"[" + A [
m.Groups[2].Value + Whatever was in group 2
"," + A ,
(m.Groups[3].Captures.Count + 1) + The number of times group 3 matched + 1
"]" A ]
私はこのオーバーロードを使用しています。これは、置換値を計算するためのデリゲートを受け入れます。