D
Dana DeLouis
A non-capturing, positive lookahead pattern seems to avoid the issue I had
with testing "12345A56789"
The key for me was to wrap the string in something we're not looking for to
avoid Beginning / Ending issues. I've borrowed Ron's CreateObject
statement.
Function Last5(Str As String)
Dim Re As Object
Dim M As Object
Dim S As String
Set Re = CreateObject("VBScript.RegExp")
With Re
.IgnoreCase = True
.Global = True
.Pattern = "\D(\d{5})(?=\D)"
S = "x" & Str & "x"
If .Test(S) Then
Set M = Re.Execute(S)
Last5 = M.Item(M.Count - 1).SubMatches(0)
Else
Last5 = "None"
End If
End With
End Function
with testing "12345A56789"
The key for me was to wrap the string in something we're not looking for to
avoid Beginning / Ending issues. I've borrowed Ron's CreateObject
statement.
Function Last5(Str As String)
Dim Re As Object
Dim M As Object
Dim S As String
Set Re = CreateObject("VBScript.RegExp")
With Re
.IgnoreCase = True
.Global = True
.Pattern = "\D(\d{5})(?=\D)"
S = "x" & Str & "x"
If .Test(S) Then
Set M = Re.Execute(S)
Last5 = M.Item(M.Count - 1).SubMatches(0)
Else
Last5 = "None"
End If
End With
End Function