将数字与字符串分开

时间:2014-05-04 21:40:44

标签: excel vba excel-vba split

是否可以通过简单的方法将第二个数字与此字符串分开(数字可能过多):

variable = Test +5 test (1e8+2) 'in this case 1

并将其分配给variable2?我试过这段代码:

temporary = split(variable)

variable2 = temporary(4) 'Now i don't know how to separate i from "(" and "e"

1 个答案:

答案 0 :(得分:1)

这是使用正则表达式返回字符串中第二个数字(或连续数字)的一种方法:

Dim variable As String
Dim variable2 As Long
Dim RE As Object
variable = "Test +5 test (1e8+2)" 'in this case 1

Set RE = CreateObject("vbscript.regexp")
With RE
    .Pattern = "\D*\d\D*(\d+).*"
    variable2 = .Replace(variable, "$1")
End With

如果第一个号码也可以是多个数字,请将第一个 \ d 更改为 \ d +