iaskyou
iaskyou

Reputation: 97

Error in using regex (verifying the code for regex)

I have this piece of code

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(() =>
                     mainWindow.richtextbox2.Document.Blocks.Add(new Paragraph(new 
                      Run("Hello")))));

This is already working but the spacing still remains in between text sent. how can I fix it or update my richtextbox? I am trying to eliminate the spacing in displaying a text to a richtextbox as shown

enter image description here

I want to show :

Hello
Hello
Hello

without the multiple newline or spacing.

Upvotes: 2

Views: 65

Answers (1)

Mihai Hantea
Mihai Hantea

Reputation: 1743

Document is not of type string.

EDIT

string myText = new TextRange(richtextbox2.Document.ContentStart, 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);

Upvotes: 2

Related Questions