VBScript Regex: Match everything except multi-line pattern

守給你的承諾、 提交于 2019-12-24 15:25:06

问题


There is a very similar question to the question I need answered (Regex / Vim: Matching everything except a pattern, where pattern is multi-line?): I need to convert the following Vim regular expression into a VBScript regular expression:

:%s/\%(^end\n*\|\%^\)\zs\_.\{-}\ze\%(^begin\|\%$\)//

Basically, what I need to do is grab all the text before, between, and after methods (not including the code within the methods). I already have a VBScript regular expression to grab methods and the code within their bodies, as below:

((?:(?:Public|Private)) (?:Sub|Function).+)\n(.*\n)*?End (?:Sub|Function)

And below is example text of both global and method code:

'-----------------------------------------------------------------------------------------
'
'   the code:   Header
'
'-----------------------------------------------------------------------------------------

Dim GLOBAL_VARIABLE_1
Dim GLOBAL_VARIABLE_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


msgbox GLOBAL_VARIABLE_1



Public Function doThat(byVal xPath)
'' Created               : dd/mm/yyyy
'' Return                : array
' 'Param            : xPath

     return = split(mid(xPath, 2), "/")
     doThat = return

End Function


GLOBAL_VARIABLE_2 = 2 + 2


Public Function alsoDoThis(byRef obj)
'' Created               : dd/mm/yyyy
'' Return                : string
' 'Param            : obj, an xml document object

     For i = 0 To 4
          return = return & "hi" & " "

     Next

     alsoDoThis = trim(return)

End Function


GLOBAL_VARIABLE_3 = 2 + 2

How can I negate or flip the VBscript regular expression I have, or convert the Vim regular expression I need, in order to grab all the global-level code before, in between, or after method-level code (excluding the method declarations and the "End Sub/Function" part)?


回答1:


Remove all procedures and functions and what remains is what you're looking for.

text = "..."

Set re = New RegExp
re.Pattern = "((public|private)\s+)?(function|sub)[\s\S]+?end\s+(function|sub)"
re.Global  = True
re.IgnoreCase = True

rest = re.Replace(text, "")


来源:https://stackoverflow.com/questions/16573708/vbscript-regex-match-everything-except-multi-line-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!