How to replace the text between two characters in c#

前端 未结 7 2175
醉梦人生
醉梦人生 2020-12-06 06:24

I am bit confused writing the regex for finding the Text between the two delimiters { } and replace the text with another text in c#,how to replace?

7条回答
  •  生来不讨喜
    2020-12-06 07:11

    The following regular expression will match the criteria you specified:

    string pattern = @"^(\<.{27})(\{[^}]*\})(.*)";
    

    The following would perform a replace:

    string result = Regex.Replace(input, pattern, "$1 REPLACE $3");
    

    For the input: "<012345678901234567890123456{sdfsdfsdf}sadfsdf" this gives the output "<012345678901234567890123456 REPLACE sadfsdf"

提交回复
热议问题