[英]How can I use a regex variable for a find and replace command in bash?
[英]Can find how to use replace with regex in VBA
我在Excel中有一張表,它的列之一包含類型的字符串:125j,0j,12j等。
我希望使用VBA將單元格(在此列中)的值替換為數字(即,我想刪除“ j”)。 這是我所擁有的:
Dim reg As Object
Set reg = CreateObject("VBScript.RegExp")
reg.IgnoreCase = True
reg.Pattern = "^[0-9]+"
If reg.test(Cells(i, 4).Value) Then
Cells(i, 4).Value = reg.Execute(Cells(i, 4).Value)
End If
但這是行不通的,有人知道如何壓縮它或從另一個命令中獲得相同的結果嗎?
謝謝
.Execute
方法的結果是MatchCollection
對象。 您可以明確地使用它:
Dim reg As Object
Dim allMatches As Object
Set reg = CreateObject("VBScript.RegExp")
reg.IgnoreCase = True
reg.Pattern = "^[0-9]+"
If reg.test(cells(i, 4).Value) Then
Set allMatches = reg.Execute(cells(i, 4).Value)
cells(i, 4).Value = allMatches(0)
End If
或隱式地:
cells(i, 4).Value = reg.Execute(cells(i, 4).Value)(0)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.