How to replace the text between two characters in c#

前端 未结 7 2176
醉梦人生
醉梦人生 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:00

    You can use the Regex expression that some others have already posted, or you can use a more advanced Regex that uses balancing groups to make sure the opening { is balanced by a closing }.

    That expression is then (?\{)([^\}]*)(?<-BRACE>\})

    You can test this expression online at RegexHero.

    You simply match your input string with this Regex pattern, then use the replace methods of Regex, for instance:

    var result = Regex.Replace(input, "(?\{)([^\}]*)(?<-BRACE>\})", textToReplaceWith);
    

    For more C# Regex Replace examples, see http://www.dotnetperls.com/regex-replace.

提交回复
热议问题