【问题标题】:Error in using regex (verifying the code for regex)使用正则表达式时出错(验证正则表达式的代码)
【发布时间】:2014-02-28 08:49:52
【问题描述】:

我有这段代码

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 时的间距,如图所示

我要展示:

Hello
Hello
Hello

没有多个换行符或空格。

【问题讨论】:

    标签: c# regex wpf


    【解决方案1】:

    文档不是字符串类型。

    编辑

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

    【讨论】:

    • 那么我应该使用什么对象?我知道 .Text 在 wpf c# 中不可用
    • 要获取 WPF 中的文本,请参见此处:stackoverflow.com/questions/957441/…
    • 是的,它正在工作,但它没有删除richtextbox 中的间距。呵呵。我怎样才能删除我?
    • 您需要用新值更新richtextbox。
    • 你想显示这样的“你好你好”还是你好前面的空格?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 2011-05-06
    • 2016-10-19
    • 2011-03-24
    相关资源
    最近更新 更多