VBScript正则表达式:匹配除多行模式之外的所有内容

时间:2013-05-15 19:45:15

标签: regex vbscript multiline except

我需要回答的问题有一个非常类似的问题(Regex / Vim: Matching everything except a pattern, where pattern is multi-line?):我需要将以下Vim正则表达式转换为VBScript正则表达式:

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

基本上,我需要做的是抓取方法之前,之间和之后的所有文本(不包括方法中的代码)。我已经有了一个VBScript正则表达式来抓取它们体内的方法和代码,如下所示:

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

以下是全局代码和方法代码的示例文本:

'-----------------------------------------------------------------------------------------
'
'   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

如何否定或翻转我所拥有的VBscript正则表达式,或者转换我需要的Vim正则表达式,以便在方法级代码之前,之间或之后获取所有全局级代码(不包括方法)声明和“结束子/功能”部分)?

1 个答案:

答案 0 :(得分:2)

删除所有程序和功能,剩下的就是您要找的东西。

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, "")
相关问题