一、写两个自定义函数:
Function TestStr(ByVal Test_str As String, ByVal match_str As String) As Boolean
'引用:Microsoft VBScript Regular Expressions 5.5
'功能:是否匹配正则表达式
'参数:Test_str--测试字符串,match_str--正则表达式
Dim re As New regexp
re.Pattern = match_str
re.IgnoreCase = True
re.Global = True
TestStr = re.Test(Test_str)
Set re = Nothing
End Function
Function ReplaceMatch(ByVal str As String, ByVal match_str As String, ByVal Rematch_str As String) As String
'引用:Microsoft VBScript Regular Expressions 5.5
'功能:替换字符串
'参数:Test_str--测试字符串,match_str--源正则表达式,Rematch_str--替换的正则表达式
Dim re As New regexp
re.Pattern = match_str
re.IgnoreCase = True
re.Global = True
ReplaceMatch = re.Replace(str, Rematch_str)
Set re = Nothing
End Function
二、在查询中引用函数
假设要处理的字段名称为F1,则:
SELECT *, IIf(teststr([F1],"([a-z]|[A-Z])"),replacematch([F1],"(^.*(?:[a-z]|[A-Z]))(\d+$)","$2"),[F1]) AS 获取的数字
FROM 表名;
三、如果还不明白,请参见《无关紧要》一文。