How to replace text between two characters in C #

I got a little confused writing a regular expression to search for text between two delimiters {} and replaced the text with another text in C #, how to replace?

I have tried this.

StreamReader sr = new StreamReader(@"C:abc.txt"); string line; line = sr.ReadLine(); while (line != null) { if (line.StartsWith("<")) { if (line.IndexOf('{') == 29) { string s = line; int start = s.IndexOf("{"); int end = s.IndexOf("}"); string result = s.Substring(start+1, end - start - 1); } } //write the lie to console window Console.Write Line(line); //Read the next line line = sr.ReadLine(); } //close the file sr.Close(); Console.ReadLine(); 

I want to replace the found text (result) with another text.

+8
source share
9 answers

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

 Regex yourRegex = new Regex(@"\{([^\}]+)\}"); string result = yourRegex.Replace(yourString, "anyReplacement"); 
+9
source
 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"); 
+8
source

To get the string between the brackets that need 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 replace the found text, you can simply use the Replace method as follows:

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

Regex template explanation:

 \{ # 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" 
+4
source

The following regular expression will match the specified criteria:

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

The following will do the replacement:

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

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

+2
source

You need two calls to Substring() , not one: one to get textBefore , and the other to textAfter , and then you combine those who have your replacement.

 int start = s.IndexOf("{"); int end = s.IndexOf("}"); //I skip the check that end is valid too avoid clutter string textBefore = s.Substring(0, start); string textAfter = s.Substring(end+1); string replacedText = textBefore + newText + textAfter; 

If you want to keep curly braces, you need a little tweaking:

 int start = s.IndexOf("{"); int end = s.IndexOf("}"); string textBefore = s.Substring(0, start-1); string textAfter = s.Substring(end); string replacedText = textBefore + newText + textAfter; 
+1
source

You can use Replace for this.

0
source

The easiest way is to use the split method if you want to avoid any kind of regular expression. This is aproach:

 string s = "sometext {getthis}"; string result= s.Split(new char[] { '{', '}' })[1]; 
0
source

You can use a Regex expression that has already been published by other users, or you can use a more advanced Regex that uses balancing groups to make sure that the opening is {balanced by closing}.

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

You can check this expression online at RegexHero .

You simply map your input string to this Regex pattern, and then use Regex replacement methods, for example:

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

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

0
source

You can use many functions to perform.

String.Replace ("Search Char", "Replace With");

or String.SubString ();

-2
source

All Articles