# RegEx Syntax Highlighting in RTB [VB.net]



## RBX (Nov 2, 2011)

There are numerous examples of Syntax Highlighting using RegEx floating over the web but all are way too length and complicated codes for someone fairly new to Vb.net.

I found this code which uses Strings


```
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

Dim words As New List(Of String)

words.Add("<html>")

words.Add("<body>")

If RichTextBox1.Text.Length > 0 Then

Dim selectStart As Integer = RichTextBox1.SelectionStart

RichTextBox1.Select(0, RichTextBox1.Text.Length)

RichTextBox1.SelectionColor = Color.Black

RichTextBox1.DeselectAll()

For Each oneWord As String In words

Dim pos As Integer = 0

Do While RichTextBox1.Text.ToUpper.IndexOf(oneWord.ToUpper, pos) >= 0

pos = RichTextBox1.Text.ToUpper.IndexOf(oneWord.ToUpper, pos)

RichTextBox1.Select(pos, oneWord.Length)

RichTextBox1.SelectionColor = Color.Blue

pos += 1

Loop

Next

RichTextBox1.SelectionStart = selectStart

End If

End Sub
```

I wanted to use Regular Expressions using something like


```
Dim words As New List(Of Regex)

words.Add(New Regex("<html.*?>", RegexOptions.IgnoreCase))
```
but nothing worked out as indexOf() or similar position finding functions would take Strings and not Expressions.


----------

