MS Access VBA中的正则表达式?
问题描述:
我绝对喜欢MS Access作为面向小范围数据驱动应用程序的RAD工具。但有一点我真的很想念.Net-Developer就是正则表达式。验证用户输入时它们确实派上用场。我真的不知道微软为什么不把它们放到标准的VBA库中。MS Access VBA中的正则表达式?
有没有办法在MS Access VBA中使用正则表达式?
答
您可以通过添加对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/
答
您可以使用CreateObject(“vbscript.regexp”)或仅引用脚本库。
我强烈推荐使用后期此绑定。 – 2010-09-23 21:33:39