|
-
Thread Starter
PowerPoster
Extract date from string
I just have myvar="gi_db_comuni-2026-07-10-b893f.zip"
how to get date?
note:
before and after the date are lenght variable
-
Re: Extract date from string
Code:
Sub main()
Dim myvar As String
Dim t() As String
Dim regex As RegExp
Dim mc As MatchCollection
Dim m As Match
Dim mydate As Date
'First way
myvar = "gi_db_comuni-2026-07-10-b893f.zip"
t = Split(myvar, "-")
mydate = DateSerial(CInt(t(1)), CInt(t(2)), CInt(t(3)))
Debug.Print mydate
'Second way - needs reference to VBScript Regular Expressions
Set regex = New RegExp
regex.Pattern = "\.*?-(\d{4}-\d{2}-\d{2})\.*"
regex.IgnoreCase = True
regex.Global = True
Set mc = regex.Execute(myvar)
If mc.Count = 1 Then
Set m = mc.Item(0)
t = Split(m.SubMatches(0), "-")
mydate = DateSerial(CInt(t(0)), CInt(t(1)), CInt(t(2)))
End If
End Sub
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Thread Starter
PowerPoster
Re: Extract date from string
 Originally Posted by zvoni
Code:
sub main()
dim myvar as string
dim t() as string
dim regex as regexp
dim mc as matchcollection
dim m as match
dim mydate as date
'first way
myvar = "gi_db_comuni-2026-07-10-b893f.zip"
t = split(myvar, "-")
mydate = dateserial(cint(t(1)), cint(t(2)), cint(t(3)))
debug.print mydate
'second way - needs reference to vbscript regular expressions
set regex = new regexp
regex.pattern = "\.*?-(\d{4}-\d{2}-\d{2})\.*"
regex.ignorecase = true
regex.global = true
set mc = regex.execute(myvar)
if mc.count = 1 then
set m = mc.item(0)
t = split(m.submatches(0), "-")
mydate = dateserial(cint(t(0)), cint(t(1)), cint(t(2)))
end if
end sub
Dim regex As RegExp
error:
USED DEFINED NOT DEFINED
Last edited by luca90; Today at 12:30 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|