MS Access 在 REPLACE 函数中使用通配符
MSAccess using a wildcard in the REPLACE function
我正在尝试做一些简单的事情,但我不明白为什么它不起作用。我是 MS Access 的新手 VBA.
我在文本框中有一个字符串:
\\p9990cdc\C$\Temp
我想把它变成:
C:\Temp
我正在尝试:
strSelectedFile = Replace(strSelectedFile, "\*\C$", "C:")
它不起作用。
不知道为什么 RegEx 也不起作用:
strSelectedFile = Replace(strSelectedFile, "\[\w]\C$", "C:")
一切都设置正确所以问题恰恰出在替换代码上,因为如果我尝试例如:
strSelectedFile = Replace(strSelectedFile, "C$", "C:")
有效并成功将 C$ 替换为 C:
\p9990cdc\C:\温度
我怎样才能完成这项工作?
非常感谢您的宝贵时间!
您可以改为使用 Mid(Instr())
查找 $
的索引并从那里获取字符串(减 1 以保留目录字母)。
strSelectedFile = Replace(Mid(strSelectedFile, InStr(strSelectedFile, "$") - 1, Len(strSelectedFile)), "$", ":")
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
使用您的输入并将 Replace
与此 LikeReplace
交换应该就可以了。
您可以只使用 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
我正在尝试做一些简单的事情,但我不明白为什么它不起作用。我是 MS Access 的新手 VBA.
我在文本框中有一个字符串:
\\p9990cdc\C$\Temp
我想把它变成:
C:\Temp
我正在尝试:
strSelectedFile = Replace(strSelectedFile, "\*\C$", "C:")
它不起作用。
不知道为什么 RegEx 也不起作用:
strSelectedFile = Replace(strSelectedFile, "\[\w]\C$", "C:")
一切都设置正确所以问题恰恰出在替换代码上,因为如果我尝试例如:
strSelectedFile = Replace(strSelectedFile, "C$", "C:")
有效并成功将 C$ 替换为 C:
\p9990cdc\C:\温度
我怎样才能完成这项工作?
非常感谢您的宝贵时间!
您可以改为使用 Mid(Instr())
查找 $
的索引并从那里获取字符串(减 1 以保留目录字母)。
strSelectedFile = Replace(Mid(strSelectedFile, InStr(strSelectedFile, "$") - 1, Len(strSelectedFile)), "$", ":")
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
使用您的输入并将 Replace
与此 LikeReplace
交换应该就可以了。
您可以只使用 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