How to extract groups of numbers from a string in vba

后端 未结 4 1014
栀梦
栀梦 2020-12-01 23:00

I have a string of the following shape:

RRP 90 AVE DE GAULLE 92800 PUTEAUX 0109781431-0149012126

The numbers might be seperated by other ch

4条回答
  •  猫巷女王i
    2020-12-01 23:29

    This will run much faster than looping

    Public Function NumericOnly(s As String) As String
        Dim s2 As String
        Dim replace_hyphen As String
        replace_hyphen = " "
        Static re As RegExp
        If re Is Nothing Then Set re = New RegExp
        re.IgnoreCase = True
        re.Global = True
        re.Pattern = "[^0-9 -]" 'includes space, if you want to exclude space "[^0-9]"
        s2 = re.Replace(s, vbNullString)
        re.Pattern = "[^0-9 ]"
        NumericOnly = re.Replace(s2, replace_hyphen)
    End Function
    

提交回复
热议问题