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

标签 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

没有多个换行符或空格。

最佳答案

文档不是字符串类型。

编辑

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

关于c# - 使用正则表达式时出错(验证正则表达式的代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22090288/

相关文章:

c# - 从 WCF 服务开始。从哪儿开始?

java - Java/Android 中的正则表达式

c# - 如何设置 WPF 窗口的宽度等于其标题栏中的内容?

c# - Dispatcher.Invoke在vs2010中工作但在vs2008中给出错误

r - 提取字符串,但有一些异常(exception)

c# - XAML 代码 IsChecked 在 ToggleButton 上触发

c# - 在文本框值更改时更改背景颜色

c# - 在 TableCell() 中的单元格之间添加空格

c# - 通过结果解释陈述

javascript - 正则表达式-仅匹配正斜杠和数字(用于日期)