VBA:REGEX LOOKBEHIND MS ACCESS 2010

时间:2014-08-14 11:25:17

标签: regex vba lookbehind

我有一个编写的函数,以便可以在MS Access中使用VBA 我想做以下

我在下面设置了我的代码。在产品完美运作之前的一切,但试图获得信息背后的回报""这很奇怪,因为当我在Notepad ++中执行它时它完全正常

所以它寻找字母MIP和3个字母代码之一(其中任何一个)

 StringToCheck = "MADHUBESOMIPTDTLTRCOYORGLEJ"

' PART 1
' If MIP appears in the string, then delete any of the following codes if they exist - DOM, DOX, DDI, ECX, LOW, WPX, SDX, DD6, DES, BDX, CMX,
' WMX, TDX, TDT, BSA, EPA, EPP, ACP, ACA, ACE, ACS, GMB, MAL, USP, NWP.
' EXAMPLE 1.  Flagged as: MADHUBESOMIPTDTLTRCOYORGLEJ, should be MADHUBESOMIPLTRCOYORGLEJ


Do While regexp(StringToCheck, "MIP(DOM|DOX|DDI|ECX|LOW|WPX|SDX|DD6|DES|BDX|CMX|WMX|TDX|TDT|BSA|EPA|EPP|ACP|ACA|ACE|ACS|GMB|MAL|USP|NWP|BBX)", False) <> ""
    ' SELECT EVERYTHING BEFORE THE THREE LETTER CODES
     strPart1 = regexp(StringToCheck, ".*^[^_]+(?=DOM|DOX|DDI|ECX|LOW|WPX|SDX|DD6|DES|BDX|CMX|WMX|TDX|TDT|BSA|EPA|EPP|ACP|ACA|ACE|ACS|GMB|MAL|USP|NWP|BBX)", False)
    ' SELECT EVERYTHING AFTER THE THREE LETTER CODES
     strPart2 = regexp(StringToCheck, "(?<=(DOM|DOX|DDI|ECX|LOW|WPX|SDX|DD6|DES|BDX|CMX|WMX|TDX|TDT|BSA|EPA|EPP|ACP|ACA|ACE|ACS|GMB|MAL|USP|NWP|BBX).*", False)
StringToCheck = strPart1 & strPart2
Loop

我使用的功能是我从互联网上取下的

Function regexp(StringToCheck As Variant, PatternToUse As String, Optional CaseSensitive As Boolean = True) As String

On Error GoTo RefErr:

Dim re As New regexp
re.Pattern = PatternToUse
re.Global = False
re.IgnoreCase = Not CaseSensitive

Dim m
For Each m In re.Execute(StringToCheck)
    regexp = UCase(m.Value)
Next

RefErr:
    On Error Resume Next

End Function

2 个答案:

答案 0 :(得分:1)

只需分两步完成:

  1. 检查字符串
  2. 中是否有MIP
  3. 如果是,请删除其他代码。
  4. 像这样:

    Sub Test()
      Dim StringToCheck As String
      StringToCheck = "MADHUBESOMIPTDTLTRCOYORGLEJ"
    
      Debug.Print StringToCheck
      Debug.Print CleanupString(StringToCheck)
    End Sub
    
    Function CleanupString(str As String) As String
      Dim reCheck As New RegExp
      Dim reCodes As New RegExp
    
      reCheck.Pattern = "^(?:...)*?MIP"
      reCodes.Pattern = "^((?:...)*?)(?:DOM|DOX|DDI|ECX|LOW|WPX|SDX|DD6|DES|BDX|CMX|WMX|TDX|TDT|BSA|EPA|EPP|ACP|ACA|ACE|ACS|GMB|MAL|USP|NWP|BBX)"
      reCodes.Global = True
    
      If reCheck.Test(str) Then
        While reCodes.Test(str)
          str = reCodes.Replace(str, "$1")
        Wend
      End If
    
      CleanupString = str
    End Function
    

    请注意(?:...)*?的目的是将字母分组为三个。


    由于VBScript正则表达式引擎确实支持预测,您当然也可以在单个正则表达式中执行:

    Function CleanupString(str As String) As String
      Dim reClean As New RegExp
    
      reClean.Pattern = "^(?=(?:...)*?MIP)((?:...)*?)(?:DOM|DOX|DDI|ECX|LOW|WPX|SDX|DD6|DES|BDX|CMX|WMX|TDX|TDT|BSA|EPA|EPP|ACP|ACA|ACE|ACS|GMB|MAL|USP|NWP|BBX)"
    
      While reClean.Test(str)
        str = reClean.Replace(str, "$1")
      Wend
    
      CleanupString = str
    End Function
    

    就个人而言,我更喜欢两步检查/删除模式,因为它更加明显,因此更易于维护。

答案 1 :(得分:0)

非RE选项:

Function DeMIPString(StringToCheck As String) As String
    If Not InStr(StringToCheck, "MIP") Then
        DeMIPString = StringToCheck
    Else
        Dim i As Long
        For i = 1 To Len(StringToCheck) Step 3
            Select Case Mid$(StringToCheck, i, 3)
                Case "MIP", "DOM", "DOX", "DDI", "ECX", "LOW", "WPX", "SDX", "DD6", "DES", "BDX", "CMX", "WMX", "TDX", "TDT", "BSA", "EPA", "EPP", "ACP", "ACA", "ACE", "ACS", "GMB", "MAL", "USP", "NWP":
                Case Else
                    DeMIPString = DeMIPString & Mid$(StringToCheck, i, 3)
            End Select
        Next
    End If
End Function