我试图替换方括号中的任何文字,包括它们 - " []"在UDF空虚的单元格中:
Function RMV(iCell As Range) As Variant
RMV = Replace(iCell.Value, "[*]", "")
End Function
但我猜星号(" *")在这里不起作用。
答案 0 :(得分:2)
要使用reGex,您可以将其用作函数,请记住enable Microsoft VBScript Regular Expression 5.5
Function RMV(iCell As Range) As Variant
Dim regEx As Object: Set regEx = CreateObject("VBScript.RegExp") 'If Error Set regEx = New regexp
Dim strPattern As String
Dim strInput As String
Dim strReplace As String
Dim strOutput As String
strPattern = "\[\]|\[.+?\]|$"
If strPattern <> "" Then
strInput = CStr(iCell.Value)
strReplace = ""
With regEx
.Global = True
.MultiLine = False
.IgnoreCase = False
.Pattern = strPattern
End With
If regEx.test(strInput) Then
RMV = regEx.Replace(strInput, strReplace)
Else
RMV = "Not matched"
End If
End If
End Function
ReGex test使用\[\]|\[.+?\]|$
表达式的位置。我也是Regex的新手,所以这个表达式可以优化。