Reputation: 26601
Part of an app I'm creating in C# replaces certain substrings in a string with a value in square brackets like [11]
. Often there can be the same value straight after - so I want to reduce the amount of text by combining them into one like [11,numberOfSame]
For example, if the string contains:
blahblah[122][122][122]blahblahblahblah[18][18][18][18]blahblahblah
The desired new string would be:
blahblah[122,3]blahblahblahblah[18,4]blahblahblah
Would anyone know how I would do this? Thanks! :)
Upvotes: 0
Views: 262
Reputation: 6372
string input = "[122][44][122]blah[18][18][18][18]blah[122][122]";
string output = Regex.Replace(input, @"((?<firstMatch>\[(.+?)\])(\k<firstMatch>)*)", m => "[" + m.Groups[2].Value + "," + (m.Groups[3].Captures.Count + 1) + "]");
Returns:
[122,1][44,1][122,1]blah[18,4]blah[122,2]
Explanation:
(?<firstMatch>\[(.+?)\])
Matches the [123] group, names group firstMatch
\k<firstMatch>
matches whatever text was that was matched by the firstMatch group and adding * matches it zero or more times, giving us our count used in the lambda.
My reference for anything Regex: http://www.regular-expressions.info/
Upvotes: 1
Reputation: 44374
Regex.Replace("blahblah[122][122][122]blahblahblahblah[18][18][18][18]blahblahblah",
@"(\[([^]]+)])(\1)+",
m => "[" + m.Groups[2].Value + "," + (m.Groups[3].Captures.Count + 1) + "]")
Returns:
blahblah[122,3]blahblahblahblah[18,4]blahblahblah
Explanation of regex:
( 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
Explanation of lambda:
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 ]
I am using this overload, which accepts a delegate to compute the replacement value.
Upvotes: 2