我想知道工作簿的模块中是否存在某种格式的代码行,在本例中是一个带有字符串文字的MsgBox。
找到pattern1
,但即使pattern2
存在于模块中也找不到。Pattern1
匹配带有字符串文字的MsgBox,带有左括号和右括号(),例如:
testString = MsgBox("Do you want to open our product page?", vbInformation + vbYesNoCancel, "myMessageBox")
testString = MsgBox("Do you want to open our product page?", vbYesNoCancel, "myMessageBox")
testString = MsgBox("Do you want to open our product page?", vbYesNoCancel)
testString = MsgBox("Do you want to open our product page?")
字符串Pattern2
匹配带有字符串文字的MsgBox,不带左括号和右括号(),例如:
MsgBox "Do you want to open our product page?", vbInformation + vbYesNoCancel, "myMessageBox"
MsgBox "Do you want to open our product page?", vbInformation + vbYesNoCancel
MsgBox "Do you want to open our product page?"
' Matches strings in all VBA modules of the specified workbook
' Add a reference to "Microsoft VBScript Regular Expressions 5.5" for early binding
Public Sub FindStringsInModules(ByVal objWorkbookToObfuscate As Workbook)
Dim vbComp As Object
Dim lineNum As Long
Dim lineText As String
Dim newLineText As String
' Iterate over all VBA modules in the provided workbook
For Each vbComp In objWorkbookToObfuscate.VBProject.VBComponents
' Process each line in the module
Dim lineCount As Long
lineCount = vbComp.codeModule.CountOfLines
For lineNum = 1 To lineCount
lineText = vbComp.codeModule.Lines(lineNum, 1)
newLineText = MatchLine(lineText)
vbComp.codeModule.ReplaceLine lineNum, newLineText
Next lineNum
Next vbComp
End Sub
Function MatchLine(ByVal lineText As String) As String
Dim regex As RegExp
Set regex = New RegExp
Dim pattern1 As String
Dim pattern2 As String
Dim pattern3 As String
' Pattern 1: Matches MsgBox with string literals, with opening and closing parenthesis ( )
pattern1 = "(?:[a-zA-Z0-9]+\s*=\s*)?MsgBox\s*\(\s*(""[^""]*"")(?:\s*,\s*[^,)]+(?:\s*\+\s*[^,)]+)*)?(?:\s*,\s*""[^""]*"")?\s*\)"
' Pattern 2: Matches MsgBox without parentheses, including additional parameters and concatenations
pattern2 = "MsgBox\s+""""[^""""]*""""(?:\s*&\s*""""[^""""]*""""|[^""""])*(?:\s*,\s*[^,""""]+)*"
pattern3 = "MsgBox\(""[^""]*""\)\s*"
' Combine patterns using alternation operator
regex.Pattern = pattern1 & "|" & pattern2 & "|" & pattern3
regex.Global = True
regex.MultiLine = True
Dim matches As MatchCollection
Set matches = regex.Execute(lineText)
' Process each match for potential transformation
If matches.Count > 0 Then
Dim match As match
For Each match In matches
Dim literal As String
literal = match.SubMatches(0)
If literal = "" Then
Debug.Print "lineText not catched:" & lineText
'Stop
End If
Next match
End If
MatchLine = lineText
End Function
的数据match.SubMatches(0)
返回literal = ""
,即使matches.Count > 0
。
P.S.当测试一个字符串时,MsgBox行需要用双引号括起来,内部双引号加倍,例如testString = "MsgBox ""Do you want to open our product page?"""
。
当测试一行HTML代码(不是字符串文字)时,MsgBox行将显示为直接写入代码中,例如MsgBox "Do you want to open our product page?"
。
1条答案
按热度按时间guz6ccqo1#
如果有匹配,
Regex.Test(Text)
返回true。下面是我如何重构代码:字符串
测试正则表达式是乏味的。regex101.com是一个流行的测试网站。
表情:
(?:[a-zA-Z0-9]+\s*=\s*)?MsgBox\s*(\s*(“[^"]”)(?:\s,\s*[^,)]+(?:\s*+\s*[^,)]+))?(?:\s,\s*"[^"]”)?\s)|MsgBox\s+""[^"“]""(?:\s&\s*""[^""]””|[^""])(?:\s*,\s*[^,""]+)| MsgBox(“[^"]”)\s*