我想从这一行中提取数字:
400.0000000000000000 2520.0000000000000000 3520.0000000000000000
数字之间的空间也可以灵活
数字的数量也很灵活
我写了这段代码:
Dim regxnum As New regexp
Dim searchtext As String
Dim matchxnum As MatchCollection
searchtext = "400.0000000000000000 2520.0000000000000000 3520.0000000000000000"
regxnum.Pattern = "\s*[0-9]*\.[0-9]*\s*"
Set matchxnum = regxnum.Execute(searchtext)
For Each Match In matchxnum
MsgBox Match
Next Match
但它只返回第一个数字(400.0000000000000000)。如果我将正则表达式更改为: (\ s [0-9] 。[0-9] \ s *)** 然后它返回总行数
我该怎么做这份工作?我应该更改我的正则表达式或VBA代码吗?
答案 0 :(得分:2)
Sub Tester()
Dim regxnum As New regexp
Dim searchtext As String
Dim matchxnum As MatchCollection, m As Match
searchtext = "400 2520.0000000000000000 3520.0000000000000000"
regxnum.Pattern = "\d{1,}\.{0,1}\d{0,}"
regxnum.Global = True '**************
Set matchxnum = regxnum.Execute(searchtext)
For Each m In matchxnum
Debug.Print m.Value
Next m
End Sub
答案 1 :(得分:0)
使用Regex.Matches它将返回所有匹配的集合。
http://msdn.microsoft.com/en-us/library/b49yw9s8.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2