如何拉出匹配正則運算式模式的字串。
String = "有些 1245:1000, 有些 45678:2000, 有些 100:1234"
我只需要 1245、45678、100 就在“:”之前
Sub short()
Dim RegEx As Object, MyString As String
Dim match1 As Variant
Set RegEx = CreateObject("VBScript.RegExp")
With RegEx
.Pattern = "^[\d\d\d\d:\d\d\d\d]"
End With
end sub
uj5u.com熱心網友回復:
您可以使用
Sub short()
Dim RegEx As RegExp, MyString As String
Dim m As Match, Matches as MatchCollection
MyString = "Someting 1245:1000, someting 45678:2000, someting 100:1234"
Set RegEx = New RegExp
With RegEx
.pattern = "\d (?=:\d{4})"
.Global = True
End With
Set Matches = RegEx.Execute(MyString)
If Matches.Count > 0 Then
For Each m In Matches
Debug.Print m.Value
Next
End If
End Sub
查看除錯輸出:

正則運算式詳細資訊:
\d- 一位或多位數字(?=:\d{4})- 與緊隨其后的:四位數位置匹配的正向前瞻
請參閱正則運算式演示。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/519604.html
下一篇:獲取點擊物件/圖片的參考
