title: Using Regex and updating text in richtextbox wpf c#
body: <p>I have here my piece of code:</p>
  
  <pre><code>    mainWindow.Dispatcher.Invoke(new Action(() =&gt;
                              mainWindow.richtextbox2.Document.Blocks.Add(new Paragraph(new Run("Hello")))));
  
     string myText = new TextRange(mainWindow.richtextbox2.Document.ContentStart, mainWindow.richtextbox2.Document.ContentEnd).Text;
  
     //replace two or more consecutive spaces with a single space, and
     //replace  two or more consecutive newlines with a single newline.
     var str = Regex.Replace(myText, @"( |\r?\n)\1+", "$1", RegexOptions.Multiline);
     mainWindow.Dispatcher.Invoke(new Action(() =&gt;
                              mainWindow.richtextbox2.Document.Blocks.Add(new Paragraph(new Run(str)))));
  </code></pre>
  
  <p>I know that Hello will be redundant at first start. I want to remove this redundancy at the same time, I also want to remove the spacing in every textline. This is the screenshot that I have taken during my 3 runs.</p>
  
  <p><img src="https://i.stack.imgur.com/a5uK0.png" alt="enter image description here"></p>
  
  <p>How can I fix this? Please modify the code. </p>
  
  <p>EDITED: Here is now the screenshot after I change the XAML of richtextbox. How can I make it start at the very first line? </p>
  
  <p><img src="https://i.stack.imgur.com/CtZep.png" alt="enter image description here"></p>