Combining substrings in C# with a custom format?

左心房为你撑大大i 提交于 2020-02-08 02:49:25

问题


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! :)


回答1:


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.




回答2:


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/



来源:https://stackoverflow.com/questions/13654250/combining-substrings-in-c-sharp-with-a-custom-format

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!