2016-06-28 59 views
2

我有一個myRegex函數從字符串中提取正則表達式。當我運行使用該函數的查詢時,我在多行上出現以下錯誤。正則表達式多行選項不被訪問識別

未找到方法或數據成員。

這是正則表達式功能:

Function myRegex(ByRef myString As String, ByVal pattern As String) As String 
    Dim rgx As New RegExp 
    Dim colMatches As MatchCollection 
    With rgx 
     .pattern = pattern 
     .ignoreCase = True 
     .Global = False 
     .Multiline = False 
     Set colMatches = .Execute(myString) 
    End With 
    If colMatches.Count > 0 Then 
     myRegex = colMatches(0).Value 
    Else 
     myRegex = "" 
    End If 
End Function 

這是我使用的查詢:

SELECT myRegex(phone,"[0-9]+") 
FROM table1 

我有以下參考庫檢查:

  • 微軟的VBScript正則表達式1.0
  • 微軟的VBScript正則表達式5.5

回答

4

下面的行

Dim rgx As New RegExp 

...匹配RegExp與定義所述第一庫類,它是

Microsoft VBScript Regular Expressions 1.0 

這是一個老版本,母鹿不支持Multiline屬性。您需要RegExp類從:

Microsoft VBScript Regular Expressions 5.5 

因此,要麼:

  • 刪除與舊的1.0基準庫中的鏈接,或
  • 限定RegExp類作爲VBScript_RegExp_55.RegExp,或
  • 使用後期結合(較慢),與CreateObject("VBScript.RegExp")
2

Yo你可以取消選中第一個VBScript正則表達式參考... 1.0版本...作爲@trincot suggests

或者,你可以取消勾選引用和使用後期綁定:

'Dim rgx As New RegExp 
Dim rgx As Object 
Set rgx = CreateObject("VBScript.RegExp") 

然而,由於您的查詢就會反覆調用的函數,你可能會注意到一個Static對象變量更好的性能。

Function myRegex(ByRef myString As String, ByVal pattern As String) As String 
    Static rgx As Object 
    Dim colMatches As Object 

    If rgx Is Nothing Then 
     ' create the RegExp object just once 
     Set rgx = CreateObject("VBScript.RegExp") 
     With rgx 
      .ignoreCase = True 
      .Global = False 
      .Multiline = False 
     End With 
    End If 
    rgx.pattern = pattern 
    Set colMatches = rgx.Execute(myString) 

    If colMatches.Count > 0 Then 
     myRegex = colMatches(0).Value 
    Else 
     myRegex = "" 
    End If 
End Function