[英]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. 我尝试了下面的代码,但它只给了我前6位数字。 How do I edit this to get multiple 6 digit numbers from the same string?
如何编辑它以从同一个字符串中获取多个6位数字?
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) 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"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.