2013-05-15 52 views
1

有一個非常類似的問題我需要回答的問題(Regex/Vim: Matching everything except a pattern, where pattern is multi-line?):我需要將以下Vim的正則表達式轉換成一個VBScript正則表達式: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正則表達式,以便在方法級代碼之前,之間或之後獲取所有全局級代碼(不包括方法聲明和「End Sub /功能「部分)?

回答

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, "") 
+0

很好,謝謝!我使用了下面的正則表達式,以便我還可以獲得函數名稱作爲子匹配(如果有人感興趣): '^((?:(?: Public | Private)\ s +)?(?:函數|分)+)[\ S \ S] +結束\ S +(?:函數|。?子)\ R $' – user2174745