Searching RichTextBox and deleting all between
I'm having a little trouble trying to figure out how to search all the text in my RichTextBox and delete everything between the parentheses (including the parentheses). I have a lot of files with different comments wrote in the parentheses that may have different text in them.
Example : This is my text in the text box (AS YOU CAN SEE HERE). I am wanting to be able to read the entire text box (This would be another comment) and delete the out.
Result: This is my text in the text box. I am wanting to be able to read the entire text box and delete the out.
The rich text box are usually longer files than just a couple lines. Any help would be gratefully appreciated.
You can use a regular expression for this:
Regex r = new Regex("\(.+?\)");
If you are using a richtextbox or a textbox:
rtb.Text = r.Replace(rtb.Text,"");
You can do this without reading the file into the richttextbox:
string filetext = File.ReadAllText("file.txt"); filetext = r.Replace(filetext,""); File.WriteAllText("file.txt",filetext);
Or in one line:
You can loop all files in a directory:
foreach(string file in Directory.GetFiles(targetDirectory) File.WriteAllText(file,r.Replace(File.ReadAllText(file),""));