我想检查提供的字符串是否包含超过十位数的数字。
exampleString = aaa123aaa12345aaaa <--- not true
exampleString = 12345678 <--- not true
exampleString = 1234567,12345,123345 <--- not true
exampleString = aaaa12345678901234aaaa124aaa <-- true
我试图做一些事情,比如将数字提取到单个字符串,然后检查它们的长度是否> 10,但我是不成功的。
任何帮助或建议?
简单的Like怎么样?
if str like "*###########*" then ...
Function IsMore10Digits(strVal)
With CreateObject("VBScript.RegExp")
.Pattern = "\d{11,}": IsMore10Digits = .Test(strVal)
End With
End Function
使用RegEx
对象:
Option Explicit
Sub TestReg()
' sub to test RegEx10Digits function
Dim exampleString As String
exampleString = "aaaa12345678901234aaaa124aaa"
MsgBox RegEx10Digits(exampleString)
End Sub
Function RegEx10Digits(RegString As String) As Boolean
' RegEx variables
Dim Reg As Object
Dim RegMatches As Variant
RegEx10Digits = False
Set Reg = CreateObject("VBScript.RegExp")
With Reg
.Global = True
.IgnoreCase = True
.Pattern = "\d{11,}" ' Match any set of 10+ digits
End With
Set RegMatches = Reg.Execute(RegString)
If RegMatches.Count >= 1 Then ' make sure there is at least 1 match
RegEx10Digits = True
End If
End Function