嘗試使用正則表達式模式
reg.Pattern = "(\D:)" ' non-digit followed by colon, as group $1
\D
指定非數字字符。 (\ D :)指定一個非數字字符後跟冒號。括號中,此匹配模式將在稍後的替換中被標識爲$1
;因爲它是第一個(也是唯一)被捕獲的組。
然後這樣替換:
newString = reg.Replace(oldString, "$1 ") 'replace captured group by itself then space
替換匹配的組($1
)由本身隨後空間:"$1 "
在只有一組的情況下的另一種在圖案(像這樣的) ,您可以刪除括號並將(僅)捕獲的組標識爲$+
。
reg.Pattern = "\D:" ' non-digit followed by colon, as the only group ($+)
newString = reg.Replace(oldString, "$+ ")
Sub Example()
Dim reg As New RegExp: reg.Global = True: reg.Pattern = "(\D:)"
Dim oldString As String: oldString = "abcd Date:01/01/17 10:18 Des:xyz"
Dim newString As String: newString = reg.Replace(oldString, "$1 ")
Debug.Print newString
End Sub
abcd Date: 01/01/17 10:18 Des: xyz
你的意思是你不想更換,如果有冒號前多少? – SJR
查看「在vba中使用正則表達式」 - 這會讓你想要你需要解決的問題。如果你找到一些例子(這裏有數百個),你仍然遇到麻煩,請回來澄清問題或發佈一個新問題。 – Dave
如何使用Regular Expresions? [如何在Microsoft Excel中使用正則表達式(正則表達式)in-cell和loops](http://stackoverflow.com/questions/22542834/how-to-use-regular-expressions-regex-in-microsoft-excel-雙胞胎和環) –