描述
我将分两步执行此操作,首先确定您的每个功能和子项。在这里,我使用参考\1
来确保我们匹配正确的结束函数或结束子。此正则表达式还获取函数名称并将其放入第 2 组。如果第 2 部分正确,则可以稍后使用
(?:Public|Private)\s+(Function|Sub)\s+([a-z0-9]*).*?End\s+\1
然后测试其中的每一个以查看它们是否包含您的变量,请注意在此测试中我使用多行匹配以确保注释字符不会出现在Global_Variable
同一行之前。这还检查GLOBAL_VARIABLE_1
是否前面没有以下任何内容
- 带或不带
_
分隔符的字母数字。这需要使用您可能在变量名中找到的所有字符进行更新。在此处包含连字符-
可能会与等式中使用的减号混淆。
- 评论字符
'
^[^']*?(?![a-z0-9][_]?|['])\bGLOBAL_VARIABLE_1
VB 第 1 部分
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim sourcestring as String = "replace with your source string"
Dim re As Regex = New Regex("(?:Public|Private)\s+(Function|Sub)\s+([a-z0-9]*).*?End\s+\1",RegexOptions.IgnoreCase OR RegexOptions.Singleline)
Dim mc as MatchCollection = re.Matches(sourcestring)
Dim mIdx as Integer = 0
For each m as Match in mc
For groupIdx As Integer = 0 To m.Groups.Count - 1
Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames(groupIdx), m.Groups(groupIdx).Value)
Next
mIdx=mIdx+1
Next
End Sub
End Module
$matches Array:
(
[0] => Array
(
[0] => Public Function doThis(byVal xml)
'' Created : dd/mm/yyyy
'' Return : string
'' Param : xml- an xml blob
return = replace(xml, "><", ">" & vbLf & "<")
GLOBAL_VARIABLE_1 = 2 + 2
doThis = return
End Function
[1] => Public Function doThat(byVal xPath)
'' Created : dd/mm/yyyy
'' Return : array
' 'Param : xPath
return = split(mid(xPath, 2), "/")
GLOBAL_VARIABLE_2 = 2 + 2
doThat = return
End Function
[2] => Public Sub butDontDoThis()
'' Created : dd/mm/yyyy
'' Return : string
' 'Param : obj
For i = 0 To 5
return = return & "bye" & " "
Next
End Sub
[3] => Public Sub alsoDoThis(byRef obj)
'' Created : dd/mm/yyyy
'' Return : string
' 'Param : obj, an xml document object
For i = 0 To 4
return = return & "hi" & " "
Next
GLOBAL_VARIABLE_1 = 1 + 1
End Sub
)
[1] => Array
(
[0] => Function
[1] => Function
[2] => Sub
[3] => Sub
)
[2] => Array
(
[0] => doThis
[1] => doThat
[2] => butDontDoThis
[3] => alsoDoThis
)
)
VB 第 2 部分
在本文中找到
Public Function doThis(byVal xml)
'' Created : dd/mm/yyyy
'' Return : string
'' Param : xml- an xml blob
return = replace(xml, "><", ">" & vbLf & "<")
GLOBAL_VARIABLE_1 = 2 + 2
doThis = return
End Function
例子
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim sourcestring as String = "replace with your source string"
Dim re As Regex = New Regex("^[^']*?GLOBAL_VARIABLE_1",RegexOptions.IgnoreCase OR RegexOptions.Multiline)
Dim mc as MatchCollection = re.Matches(sourcestring)
Dim mIdx as Integer = 0
For each m as Match in mc
For groupIdx As Integer = 0 To m.Groups.Count - 1
Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames(groupIdx), m.Groups(groupIdx).Value)
Next
mIdx=mIdx+1
Next
End Sub
End Module
$matches Array:
(
[0] => Array
(
[0] => Param : xml- an xml blob
return = replace(xml, "><", ">" & vbLf & "<")
GLOBAL_VARIABLE_1
)
)
在此文本中未找到
Public Function doThis(byVal xml)
'' Created : dd/mm/yyyy
'' Return : string
'' Param : xml- an xml blob
return = replace(xml, "><", ">" & vbLf & "<")
' GLOBAL_VARIABLE_1 = 2 + 2
doThis = return
End Function
例子
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim sourcestring as String = "replace with your source string"
Dim re As Regex = New Regex("^[^']*?GLOBAL_VARIABLE_1",RegexOptions.IgnoreCase OR RegexOptions.Multiline)
Dim mc as MatchCollection = re.Matches(sourcestring)
Dim mIdx as Integer = 0
For each m as Match in mc
For groupIdx As Integer = 0 To m.Groups.Count - 1
Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames(groupIdx), m.Groups(groupIdx).Value)
Next
mIdx=mIdx+1
Next
End Sub
End Module
Matches Found:
NO MATCHES.
在此文本中也找不到
Public Sub butDontDoThis()
'' Created : dd/mm/yyyy
'' Return : string
' 'Param : obj
For i = 0 To 5
return = return & "bye" & " "
Next
End Sub
例子
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim sourcestring as String = "Public Sub butDontDoThis()
'' Created : dd/mm/yyyy
'' Return : string
' 'Param : obj
For i = 0 To 5
return = return & ""bye"" & "" ""
Next
End Sub"
Dim re As Regex = New Regex("^[^']*?GLOBAL_VARIABLE_1",RegexOptions.IgnoreCase OR RegexOptions.Multiline)
Dim mc as MatchCollection = re.Matches(sourcestring)
Dim mIdx as Integer = 0
For each m as Match in mc
For groupIdx As Integer = 0 To m.Groups.Count - 1
Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames(groupIdx), m.Groups(groupIdx).Value)
Next
mIdx=mIdx+1
Next
End Sub
End Module
Matches Found:
NO MATCHES.
免责声明
有很多边缘情况可能会导致这种情况发生,例如,如果您有评论,' end function
或者如果您将字符串值分配给变量,例如thisstring = "end sub"
是的,我意识到 OP 是针对 VBscript 的,我已经包含了这些示例来演示整体逻辑以及正则表达式的工作原理。