7,763
社区成员
发帖
与我相关
我的任务
分享
Sub Test()
Dim i As Integer
Do While ReplaceStr("rich", CStr(i + 1) & ".")
i = i + 1
Loop
End Sub
Function ReplaceStr(ByVal findText As String, ByVal RepStr As String) As Boolean
Dim Rng As Range
Set Rng = ThisDocument.Content
With Rng.Find
.ClearFormatting
.Text = findText
.Replacement.ClearFormatting
.Replacement.Text = RepStr
.Execute Replace:=wdReplaceOne, Forward:=True
ReplaceStr = .Found
End With
End Function
Sub test()
Dim w1 As String
w1 = "we.r186.er5.re5.r"
Debug.Print RegFindFH(w1)
Debug.Print RevFindFH(w1)
End Sub
Function RevFindFH(ByVal bStr As String) As String
''反向查找
Dim i As Long, k As Long, w1 As String, w2 As String
k = Len(bStr)
Do
i = InStrRev(bStr, ".", k, vbTextCompare)
If i = 0 Then Exit Do
k = i
Do
w2 = Mid$(bStr, k - 1, 1)
If InStr(1, "0123456789", w2, vbTextCompare) = 0 Then Exit Do
k = k - 1
Loop
w2 = Mid$(bStr, k, i - k + 1)
If w2 <> "." Then
'''此时可以在k前面插入换行符等操作
w1 = w2 & IIf(w1 <> "", "|", "") & w1
Else
k = k - 1
End If
Loop
RevFindFH = w1
End Function
Function RegFindFH(ByVal bStr As String) As String
''正则匹配
Dim Reg As New RegExp, Match, Matchs
Dim w1 As String
With Reg
.Pattern = "[0-9]{1,}\."
.IgnoreCase = True
.Global = True
Set Matchs = .Execute(bStr)
For Each Match In Matchs
w1 = w1 & IIf(w1 <> "", "|", "") & Match.Value
Next
RegFindFH = w1
End With
End Function