2010-09-22 95 views
12

我絕對喜歡MS Access作爲面向小範圍數據驅動應用程序的RAD工具。但有一點我真的很想念.Net-Developer就是正則表達式。驗證用戶輸入時它們確實派上用場。我真的不知道微軟爲什麼不把它們放到標準的VBA庫中。MS Access VBA中的正則表達式?

有沒有辦法在MS Access VBA中使用正則表達式?

回答

21

您可以通過添加對Microsoft VBScript正則表達式庫的引用來使用VBScript Regex Object

用法示例:

Dim szLine As String 
Dim regex As New RegExp 
Dim colregmatch As MatchCollection 

With regex 
    .MultiLine = False 
    .Global = True 
    .IgnoreCase = False 
End With 

szLine = "Analyzed range (from-to) 10 100" 

regex.Pattern = "^Analyzed range" 
If regex.Test(szLine) Then 
    regex.Pattern = ".*?([0-9]+).*?([0-9]+)" 
    Set colregmatch = regex.Execute(szLine) 

    'From 
    Debug.Print colregmatch.Item(0).submatches.Item(0) 
    'To 
    Debug.Print colregmatch.Item(0).submatches.Item(1) 
End If 

來源:http://mark.biek.org/blog/2009/01/regular-expressions-in-vba/

+3

我強烈推薦使用後期此綁定。 – 2010-09-23 21:33:39

2

您可以使用CreateObject(「vbscript.regexp」)或僅引用腳本庫。