简体   繁体   中英

VBA find a string within a string based on character format

Have a problem which i cannot seem to beat. Have tried various inStr methods etc but no luck.

So I have a string which contains something like "texttext email@email.com 12/12/2015 1234567891 PST"

The problem is that I need to extract the "1234567891" based on the fact that it is numbers and there is 10 of them, no permanent positions within the text, everything can be in a different place each time around.

Is there a way to do this? My searches led me to splitting the string with " " but I can't figure out how to compare the number to "##########" format to get the result.

Consider:

Sub whatever()
    s = "texttext email@email.com 12/12/2015 1234567891 PST"
    ary = Split(s, " ")
    For Each a In ary
        If IsNumeric(a) And Len(a) = 10 Then MsgBox a
    Next a
End Sub

As an addendum to my comment,

Function tenDigits(str As String)
    Static rgx As Object

    If rgx Is Nothing Then
        Set rgx = CreateObject("VBScript.RegExp")
    End If

    With rgx
        .Global = False
        .Pattern = "[0-9]{10}"
        If .Test(str) Then tenDigits = .Execute(str)(0)
    End With

End Function

Note that the length is enclosed by braces , not brackets as I originally stated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM