How to replace the text between two characters in c#

前端 未结 7 2011
醉梦人生
醉梦人生 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 06:48

    Use Regex with pattern: \{([^\}]+)\}

    Regex yourRegex = new Regex(@"\{([^\}]+)\}");
    string result = yourRegex.Replace(yourString, "anyReplacement");
    
    0 讨论(0)
  • 2020-12-06 06:59

    To get the string between the parentheses to be replaced, use the Regex pattern

        string errString = "This {match here} uses 3 other {match here} to {match here} the {match here}ation";
        string toReplace =  Regex.Match(errString, @"\{([^\}]+)\}").Groups[1].Value;    
        Console.WriteLine(toReplace); // prints 'match here'  
    

    To then replace the text found you can simply use the Replace method as follows:

    string correctString = errString.Replace(toReplace, "document");
    

    Explanation of the Regex pattern:

    \{                 # Escaped curly parentheses, means "starts with a '{' character"
            (          # Parentheses in a regex mean "put (capture) the stuff 
                       #     in between into the Groups array" 
               [^}]    # Any character that is not a '}' character
               *       # Zero or more occurrences of the aforementioned "non '}' char"
            )          # Close the capturing group
    \}                 # "Ends with a '}' character"
    
    0 讨论(0)
  • 2020-12-06 07:00
    string s = "data{value here} data";
    int start = s.IndexOf("{");
    int end = s.IndexOf("}", start);
    string result = s.Substring(start+1, end - start - 1);
    s = s.Replace(result, "your replacement value");
    
    0 讨论(0)
  • 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>\{)([^\}]*)(?<-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>\{)([^\}]*)(?<-BRACE>\})", textToReplaceWith);
    

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

    0 讨论(0)
  • 2020-12-06 07:05

    the simplest way is to use split method if you want to avoid any regex .. this is an aproach :

    string s = "sometext {getthis}";
    string result= s.Split(new char[] { '{', '}' })[1];
    
    0 讨论(0)
  • 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"

    0 讨论(0)
提交回复
热议问题