Answered by:
Search RTB using regular expressions.

Question
-
Can anyone show me how to search for text inside a RichTextBox using regular expressions (Regex)?
Thanks.Tuesday, June 3, 2008 1:42 PM
Answers
-
Try this which is more "powerful"!smile...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim word As String = "ciao" If Me.RichTextBox1.Text.Contains(word) Then For Each MyRow In Me.RichTextBox1.Text Dim i = Me.RichTextBox1.Find(word, RichTextBoxFinds.None) If i >= 0 Then Me.RichTextBox1.SelectionStart = i Me.RichTextBox1.SelectionLength = word.Length Me.RichTextBox1.SelectionColor = Color.Blue End If Next Me.RichTextBox1.Focus() Else MsgBox("Text not found.") End If
Best regards.
Ale N.
Many enemies means much honor.- Marked as answer by Martin Xie - MSFT Monday, June 9, 2008 9:58 AM
Tuesday, June 3, 2008 5:27 PM
All replies
-
I don't know how to do it that way but you can try this just to see how it works if you don't already know it:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If Me.RichTextBox1.Text.Contains("ciao") Then Dim a As String a = Me.RichTextBox1.Find("ciao") Me.RichTextBox1.Select(a, Me.RichTextBox1.Text.Length) Me.RichTextBox1.Focus() Else MsgBox("Text not found.") End If End Sub
Hope it's helpful.
Ale N.
Many enemies means much honor.Tuesday, June 3, 2008 2:15 PM -
thanks man. i'll try it out and if it does everything am supposed to do better than regex, i'm with it for sure. :DTuesday, June 3, 2008 2:29 PM
-
Ok, so tell me if you have got problems; I'll try to help you.
Best regards.
Ale N.
Many enemies means much honor.Tuesday, June 3, 2008 3:31 PM -
eer.. it wierd but now that you mention it... i DO have a bit of a problem. i wrote the code as you have, but instead of the procedure selecting the matching string, it selects all the text within the RTB. i dunno if i'm the one entering the code wrongly. I think am a bit lost from the a=me.RichTextBox1.Find(Ciao) statement.
Would you mind breaking it down a bit?
Thanks.Tuesday, June 3, 2008 3:48 PM -
Try this which is more "powerful"!smile...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim word As String = "ciao" If Me.RichTextBox1.Text.Contains(word) Then For Each MyRow In Me.RichTextBox1.Text Dim i = Me.RichTextBox1.Find(word, RichTextBoxFinds.None) If i >= 0 Then Me.RichTextBox1.SelectionStart = i Me.RichTextBox1.SelectionLength = word.Length Me.RichTextBox1.SelectionColor = Color.Blue End If Next Me.RichTextBox1.Focus() Else MsgBox("Text not found.") End If
Best regards.
Ale N.
Many enemies means much honor.- Marked as answer by Martin Xie - MSFT Monday, June 9, 2008 9:58 AM
Tuesday, June 3, 2008 5:27 PM