简体   繁体   中英

how to extract multiple six digit numbers in a text string using VBA

I tried the code below but it only give me the first 6 digit number. How do I edit this to get multiple 6 digit numbers from the same string?

Function SixDigit(S As String, Optional index As Long = 0) As String
    Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
With RE
    .Pattern = "(?:\b|\D)(\d{6})(?:\b|\D)"
    .Global = True
        SixDigit = .Execute(S)(index).submatches(0)
End With
End Function

Use the code below, source:

https://msdn.microsoft.com/en-us/library/tdte5kwf(v=vs.84)

Function RegExpTest(patrn, strng)
Dim regEx, Match, Matches, s

' Create the regular expression.
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = True
regEx.Global = True

' Do the search.
Set Matches = regEx.Execute(strng)

' Iterate through the Matches collection.
s = ""
For Each Match in Matches
  s = s & "Match found at position "
  s = s & Match.FirstIndex & ". "
  s = s & "Match Value is '"
  s = s & Match.Value & "'."
  s = s & vbCRLF
Next

RegExpTest = s
End Function
MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))

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