正则表达式:
^(?:public|private)? *?(sub|function|Property (?:Let|Set|Get)) +?(\w+)\(?
vb的处理方法:
Option Explicit Private Sub Form_Load() Dim strData$ Dim reg As Object Dim matchs As Object, match As Object strData = "Option Explicit" & vbCrLf & _ "Public FormDisplayed As Boolean" & vbCrLf & _ "Public VBInstance As VBIDE.VBE" & vbCrLf & _ "Dim mcbMenuCommandBar As Office.CommandBarControl" & vbCrLf & _ "Public WithEvents MenuHandler As CommandBarEvents 'command bar event handler" & vbCrLf & _ "" & vbCrLf & _ "'this event fires when the menu is clicked in the IDE" & vbCrLf & _ "Private Sub MenuHandler_Click(ByVal CommandBarControl As Object, handled As Boolean, CancelDefault As Boolean)" & vbCrLf & _ "'Dim dd As Object, cc As Object, s$, i" & vbCrLf & _ "'Set dd = VBInstance.CommandBars" & vbCrLf & _ "'" & vbCrLf & _ "'s = VBInstance.CommandBars.Count & vbCrLf" & vbCrLf & _ "'For i = 10 To VBInstance.CommandBars.Count - 1" & vbCrLf & _ "' Set cc = VBInstance.CommandBars(i)" & vbCrLf & _ "' s = s & cc.Index & vbTab & cc.Name & vbTab & cc.NameLocal & vbCrLf" & vbCrLf & _ "'Next" & vbCrLf & _ "" & vbCrLf & _ "' VBInstance.ActiveCodePane.CodeModule.InsertLines 5, ""fadsfsadfsdafsdaf""" & vbCrLf & _ "MsgBox VBInstance.ActiveCodePane.CodeModule.CountOfLines" Set reg = CreateObject("vbscript.regexp") reg.Global = True reg.IgnoreCase = True reg.Pattern = "(?:public|private)? *?(?:sub|function) +?(\w+)\(?" Set matchs = reg.Execute(strData) For Each match In matchs Debug.Print match.SubMatches(0) Next End Sub