|
Sub test()
Dim Str As String
Dim M, N As Integer
Dim RegEx As Object
Dim mMatch As Match
Dim MCol As MatchCollection
Str = "20A+2^2[a2][a32]-14/2BC-{sin(1.5708)}*10[a2]"
[a1] = Str
Set RegEx = CreateObject("vbscript.regexp")
With RegEx
.Global = True
.Pattern = "\[(.*?)\]|\{(.*?)\}|[^0-9\.\+\-\*\/\^\%\(\)]"
Set MCol = .Execute(Str)
End With
For Each mMatch In MCol
M = 0
Do
N = InStr(Right(Str, Len(Str) - M), mMatch.Value)
If Left(mMatch.Value, 1) = "{" Then
[a1].Characters(Start:=M + N, Length:=Len(mMatch.Value)).Font.Color = vbRed
Else
[a1].Characters(Start:=M + N, Length:=Len(mMatch.Value)).Font.Color = vbBlue
End If
M = M + N + Len(mMatch.Value) - 1
Loop While InStr(Right(Str, Len(Str) - M), mMatch.Value) > 0
Next mMatch
End Sub
这个容错度高些
[ 本帖最后由 kevinchengcw 于 2010-6-29 19:13 编辑 ] |
|