Question : Regex and VB.NET - Richtext selection

When a user clicks on a rich textbox and the selected index of the spot they've clicked on is between 2 brackets ([ and ]), I want to highlight the whole bracketed text.  I could do this with a convoluted string-parse, but was wondering if anyone had an idea how to write a regex to get the beginning/ending of the brackets?  I would guess to try to match a "[" without a "]" before the clicked/selected spot and then look for the 1st closing "]" after...  Any ideas on how to write the regex?

Answer : Regex and VB.NET - Richtext selection

Hmmm.... don't see the code above, so:

Private Sub RichTextBox1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseClick
      Dim reg As New Regex("\[[^\]]+]")
      Dim matches As MatchCollection
      Dim box As RichTextBox
      Dim i As Integer

      box = DirectCast(sender, RichTextBox)

      matches = reg.Matches(Me.RichTextBox1.Text)

      For Each mat As Match In matches
            If box.SelectionStart > mat.Index AndAlso box.SelectionStart < mat.Index + mat.Length Then
                  i = box.SelectionStart
                  box.Select(mat.Index + 1, mat.Length - 2)
                  box.SelectionBackColor = Color.AliceBlue
                  box.Select(i, 0)
                  Exit For
            End If
      Next

End Sub
Random Solutions  
 
programming4us programming4us