指定某字符,并将该字符连续部分数目替换为指定的1个字符。
如调用:RegexReplace("ABC%%%%%DE%%FGHIJK%%","%","*")
结果将得到:ABC*DE*FGHIJK*
'传入:%%%%exe%%%%%%%.dll '结果:%exe%.dll
'by Gary 20130308
Private Function RegexReplace(ByVal Expression As String, Optional ByVal Find As Variant = "%", Optional ByVal Replace As Variant = "%") As String On Error GoTo ErrHandler Dim rtStr As String, arrExpression() As String Dim I As Integer If Len(Trim(Expression)) <= 0 Then Exit Function arrExpression() = Split(Expression, Find) '如: 传入的 Expression 变量值为:%exe%%txt%%% 得到的结果应该为:%exe%txt% '当第一个字符为 Find 传入的字符时,不会加在字符串中,会被过滤掉。此句就解决了这个问题 If Left(Expression, 1) = Find Then rtStr = Find For I = 0 To UBound(arrExpression) If arrExpression(I) <> "" Then If I = UBound(arrExpression) Then rtStr = rtStr & arrExpression(I) Else rtStr = rtStr & arrExpression(I) & Find End If End If Next Erase arrExpression() RegexReplace = rtStr Exit Function ErrHandler: RegexReplace = "" End Function