Richtextbox : split text into paragraphs

Xancholy

Well-known member
Joined
Aug 29, 2006
Messages
143
Programming Experience
Beginner
Please can someone tell me what regex to use to:

Split richtextbox1.text into paragraphs (where blank line is found)


I have tried "\r\n\r\n" but it does not seem to work on richtextbox.

VB.NET:
        For Each para As String In System.Text.RegularExpressions.Regex.Split(Richtextbox1.Text, ("\r\n\r\n"))
            'MessageBox.Show(para)
Next

Why does this regex work in a textbox but not in a richtextbox ?

Thank you
 
It's because there's no ControlChars.CrLf in the richtextbox only a ControlChars.Lf. If you're looking by the string change it to "\n\n".

Better yet:

VB.NET:
For Each para As String In System.Text.RegularExpressions.Regex.Split(RichTextBox1.Text, ControlChars.Lf & ControlChars.Lf)
    MessageBox.Show(para)
Next
 
Matt, you're 2 for 2 in 2 days. Thanks very much, that worked fine.

The following works fine in a multiline textbox.
VB.NET:
        'strip trailing blankspace
        Me.TextBox1.Text = Regex.Replace(TextBox1.Text, "\\s+\r\n", "\r\n")
        'strip trailing tabs
        Me.TextBox1.Text = Regex.Replace(TextBox1.Text, "\t", "")
        'remove trailing blank lines
        Me.TextBox1.Text = Regex.Replace(TextBox1.Text, "\r\n\r\n", "\r\n")

What would be the richtextbox equivalent to:
1. strip all trailing blanks after every line
2. remove all trailing blank lines
3. remove blank double lines
 
It looks like you're expecting a \r\n between lines of the multiline textbox. In the richtextbox you'd just be seeing \n.
 
Hmmm. This doesn't work...
VB.NET:
        'strip trailing blankspace
        Me.RichTextBox1.Text = Regex.Replace(Me.RichTextBox1.Text, "\\s+\n", "")

I still haven't figured how to remove multiple trailing linefeeds between paras and replace with single linefeed.
 
If richtextbox.text appears as:
VB.NET:
blah blah blah blah blah 
blah blah blah blah blah 
blah blah blah 


blah blah blah blah 
blah blah blah blah 



blah blah blah blah 
blah blah blah 
blah blah blah

For my purposes a paragraph is a block of text at start/end of file with a blank line above and/or beneath

How can I retain only 1 blank line in between paras to get the above to appear neatly as:
VB.NET:
blah blah blah blah blah 
blah blah blah blah blah 
blah blah blah 

blah blah blah blah 
blah blah blah blah 

blah blah blah blah 
blah blah blah 
blah blah blah
 

Latest posts

Back
Top