Hi,
i want to parse VB6 code via Regex. However being new to Regex I have encountered a few problems concerning the regexes to use. Currently, I have problems recognizing these constructs:
' Subs
' Sub Test
Private Sub Test(ByVal x as Integer)
'Private Sub Test(ByVal y as Integer)
Dim dummy as String
dummy = "Private Sub Test(ByVal y as Integer)"
End Sub
I have basically these 2 problems: How do I write a Regex that matches the Sub definition, and includes the all commment (and empty) lines above its definition? And how can I prevent that the Sub definitions which are either disabled by comment or included in strings aren't matched? Plus I need to support definitions which are spanned over multiple lines, like this one:
' Subs
' Sub Test
Private Function Test2( _
ByVal x as Integer _
) As Long
'Private Sub Test(ByVal y as Integer)
Dim dummy as String
dummy = "Private Sub Test(ByVal y as Integer)"
End Function
Any hint would be greatly appreaciated. The solutions I've come up with don't work with multiple lines or capture more than just one Sub definition. It then just matches to the end of the last End Sub occurrence due to greedy matching.
My try in C# currently looks like this:
(('(?<comment>[\S \t]+[\n\r]+))*((?<accessmodifier>(Private|Public))\s+_?)(?<functiontype>(Sub|Function))\s+_?(?<name>[\S]+)\((?<parameters>[\S \t]*)\)([ \t]+As[ \t]+(?<returntype>\w+))?)|(?<endfunction>End (Sub|Function))
I'm using Multiline
, Singleline
, IgnoreCase
, ExplicitCapture
.
Thanks for your help!