使用正则表达式时出错(验证正则表达式的代码)

时间:2014-02-28 08:49:52

标签: c# regex wpf

我有这段代码

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")))));

这已经有效,但间距仍然保留在发送的文本之间。 我该如何修复它或更新我的richtextbox?我试图消除显示文本到richtextbox的间距,如图所示

enter image description here

我想表明:

Hello
Hello
Hello

没有多个换行符或间距。

1 个答案:

答案 0 :(得分:2)

文档不是字符串类型。

修改

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);