代码之家  ›  专栏  ›  技术社区  ›  Rakha

在REPLACE函数中使用通配符的MSAccess

  •  0
  • Rakha  · 技术社区  · 6 年前

    我试着做一些简单的事情,但我不明白为什么它不起作用。我对微软Access VBA很陌生。

    \\p9990cdc\C$\Temp公司

    我想把它变成:
    C: \Temp温度

    我在努力:

    strSelectedFile = Replace(strSelectedFile, "\\*\C$", "C:")
    

    也没用。

    strSelectedFile = Replace(strSelectedFile, "\\[\w]\C$", "C:")
    

    所有的设置都正确,所以问题就出在替换代码上,因为如果我尝试例如:

    strSelectedFile = Replace(strSelectedFile, "C$", "C:")
    

    它工作正常,并成功地用C替换了C$:

    \p9990cdc\C:\温度

    我该怎么做?

    非常感谢你的时间!

    3 回复  |  直到 6 年前
        1
  •  1
  •   Marcucciboy2 Scott Craner    6 年前

    你可以用 Mid(Instr()) $ 然后从那里获取字符串(减去1以保留目录字母)。

    strSelectedFile = Replace(Mid(strSelectedFile, InStr(strSelectedFile, "$") - 1, Len(strSelectedFile)), "$", ":")
    
        2
  •  2
  •   Erik A    6 年前

    Replace 不做通配符。您可以实现自己的函数,也可以使用 VBScript.RegEx .

    Public Function LikeReplace(strInput As String, strPattern As String, strReplace As String, Optional start As Long = 1)
        Dim LenCompare As Long
        Do While start <= Len(strInput)
            For LenCompare = Len(strInput) - start + 1 To 1 Step -1
                If Mid(strInput, start, LenCompare) Like strPattern Then
                    strInput = Left(strInput, start - 1) & strReplace & Right(strInput, Len(strInput) - (start + LenCompare - 1))
                End If
            Next
            start = start + 1
        Loop
        LikeReplace = strInput
    End Function
    

    使用输入和交换 替换 带着这个 LikeReplace

        3
  •  2
  •   Krish    6 年前

    你可以用 VBScript.RegEx 正确的模式。

    Public Function ReplacePattern(ByRef iString As String, iSearchPattern As String, iReplaceWith As Variant)
    '---------------------------------------------------------------------------------------
    ' Procedure : ReplacePattern
    ' Purpose   : Searches a string by pattern and replaces the text with given value
    ' Returns   : Replaced string.
    '---------------------------------------------------------------------------------------
    '
    
        Dim RE As Object
        Set RE = CreateObject("VBScript.RegExp")
    
        RE.ignorecase  = True 
        RE.Global      = True
    
        RE.Pattern     = iSearchPattern
        iString        = RE.Replace(iString, iReplaceWith)
        ReplacePattern = iString
    
        On Error Resume Next
        Set RE = Nothing
    
    End Function
    

    阅读更多关于模式的信息 Here

    "^\\\\.*C\$" =>以“\\+除换行符+C以外的任意字符数开头$

    使用

    ? ?replacepattern("\\p9990cdc\C$\Temp","^\\\\.*C\$","C:") C:\Temp