我想通過Regex解析VB6代碼。然而對於正則表達式來說我還是遇到了一些關於正則表達式的問題。目前,我有問題,認識到這些結構:正則表達式跨越多行/識別註釋行
' 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
我已經基本上這2個問題:如何編寫,小組定義相匹配,包括它的定義之上的所有commment(空)線的正則表達式?我怎樣才能防止被評論禁用或包含在字符串中的子定義不匹配? 另外,我需要支持這些跨越多行的定義,像這樣的:
' 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
任何暗示將不勝appreaciated。我提出的解決方案不適用於多行或捕獲不止一個Sub定義。然後由於貪婪匹配而匹配到最後End Sub的結尾。
我在C#中的嘗試目前看起來是這樣的:
(('(?<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))
我使用Multiline
,Singleline
,IgnoreCase
,ExplicitCapture
。
感謝您的幫助!