【问题标题】:Regex multiline option is not recognized by access访问无法识别正则表达式多行选项
【发布时间】:2017-05-23 07:24:00
【问题描述】:

我有一个 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

我检查了以下参考库:

  • Microsoft VBScript 正则表达式 1.0
  • Microsoft VBScript 正则表达式 5.5

【问题讨论】:

    标签: ms-access vba


    【解决方案1】:

    下面一行

    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】:

      您可以取消选中第一个 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 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-04
        • 1970-01-01
        • 1970-01-01
        • 2023-01-16
        相关资源
        最近更新 更多