用正则表达式提取数字

时间:2013-06-03 17:36:01

标签: regex vba

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

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代码吗?

2 个答案:

答案 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)