0

我想从即这一行中提取数字:

400.00000000000000000 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*)**那么它返回总行

我怎么能做这份工作?我应该更改我的 Regex 或 VBA 代码吗?

4

2 回答 2

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
于 2013-06-03T17:57:23.977 回答
0

使用 Regex.Matches 它将返回所有匹配项的集合。

http://msdn.microsoft.com/en-us/library/b49yw9s8.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2

于 2013-06-03T17:48:26.673 回答