如果字符串包含长(+10位)数字,则返回true

问题描述 投票:0回答:3

我想检查提供的字符串是否包含超过十位数的数字。

exampleString = aaa123aaa12345aaaa   <--- not true
exampleString = 12345678   <--- not true
exampleString = 1234567,12345,123345   <--- not true
exampleString = aaaa12345678901234aaaa124aaa <-- true

我试图做一些事情,比如将数字提取到单个字符串,然后检查它们的长度是否> 10,但我是不成功的。

任何帮助或建议?

regex vba excel-vba excel
3个回答
3
投票

简单的Like怎么样?

if str like "*###########*" then ...

1
投票
Function IsMore10Digits(strVal)
    With CreateObject("VBScript.RegExp")
        .Pattern = "\d{11,}": IsMore10Digits = .Test(strVal)
    End With
End Function

0
投票

使用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
© www.soinside.com 2019 - 2024. All rights reserved.