VBA根据字符格式在string中查找string
有一个问题,我似乎无法击败。 已经尝试了各种inStr方法等,但没有运气。
所以我有一个string,其中包含“texttext email@email.com 12/12/2015 1234567891 PST”
问题是我需要根据数字提取“1234567891”,其中有10个,在文本中没有固定位置,每次都可以在不同的地方。
有没有办法做到这一点? 我的search导致我分割string“”,但我不知道如何比较数字“##########”格式来获得结果。
考虑:
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
作为我的评论的附录,
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
请注意,长度由括号括起来 ,而不是像我最初所说的括号 。