You can combine the use of Regular Express and RichTextBox functionality to locate the occurrence of expressions in a string and at the same time capture the line number of the lines containing the expressions. Regular Expressions are a powerful addition to the developers toolkit in .NET. However, with a little ingenuity, you can combine the functionality of the RichTextBox Class to make them even more productive.
To illustrate this, I am going to create an instance of the RichTextBox, and call it "rtb".
Public Shared rtb As New Windows.Forms.RichTextBox
Next, I will place the following text into the textbox. Please disrerard the fact that it appears to be a mixture of VB Code and some other syntax, Setup " Test Set 1 ", I have placed this syntax in the RichTextBox Text for demonstration purposes.
Public Class Test
Setup " Test Set 1 "
' some code
Public Sub Test()
End Sub
Setup "Test Set 2"
' some more code
Finally, I will call the Sub shown below. This method creates a MatchCollection of all of the expressions, enclosed in quotes, in the lines that start with the word "Setup".
| Public Sub EnumerateExpressions() Dim sExp As String = "^[ \t]*Setup "" ?(?<name>[^""]+) ?""" Dim m As Match Dim mc As MatchCollection = _ Regex.Matches(rtb.Text.Replace(vbCrLf, vbLf), _ sExp, RegexOptions.Multiline Or RegexOptions.IgnoreCase) For Each m In mc Debug.WriteLine(m.Groups("name").Value) Debug.WriteLine(m.Index) Debug.WriteLine(rtb.GetLineFromCharIndex(m.Index)) Next End Sub |
| Dim mc As MatchCollection = _ Regex.Matches(rtb.Text.Replace(vbCrLf, vbLf), _ sExp, RegexOptions.Multiline Or RegexOptions.IgnoreCase) |