Here is my code:
Dim StartString As String = "Private Sub"
Dim EndString As String = "End Sub"
Dim SearchString As String = StartString & "(.+)" & EndString
Dim Data() As String = New Regex(SearchString).Matches(Text).Cast(Of Match).Select(Function(m) m.Groups(1).Value).ToArray
This is the value of Text:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As Integer
x = 10
MessageBox.Show(x / "Simon")
End Sub
Private Function Test()
Dim x As Integer
x = 30
MessageBox.Show(x / "Test")
End Function
End Class
How come my Data array is not populated with any data?
I suspect it’s because you are attempting to make your pattern match over multiple lines.
The
.character matches anything except newline\n, and the body of thePrivate Sub/End Subyou want to match has newlines in it.You have to enable the regex option
SingleLine(see the msdn for regular expressions):So, try
New Regex(SearchString,RegexOptions.SingleLine)instead ofNew Regex(SearchString).