jessiejane
Programmer
I have a string "John-2/3/2010". How would I get just the date from the string.
Given: John-2/3/2010
Output: 2/3/2010
Given: John-2/3/2010
Output: 2/3/2010
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
[COLOR=#0000ff]'# Example for using regular expressions in VBscript[/color]
string01 [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"John-2/3/2010"[/color]
wscript[COLOR=#804040][b].[/b][/color]echo string01 [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]" -> "[/color] [COLOR=#804040][b]&[/b][/color] extract_date[COLOR=#804040][b]([/b][/color]string01[COLOR=#804040][b])[/b][/color]
string02 [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"Mary-05/01/2010"[/color]
wscript[COLOR=#804040][b].[/b][/color]echo string02 [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]" -> "[/color] [COLOR=#804040][b]&[/b][/color] extract_date[COLOR=#804040][b]([/b][/color]string02[COLOR=#804040][b])[/b][/color]
string03 [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"abc xyz 29/6/2010"[/color]
wscript[COLOR=#804040][b].[/b][/color]echo string03 [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]" -> "[/color] [COLOR=#804040][b]&[/b][/color] extract_date[COLOR=#804040][b]([/b][/color]string01[COLOR=#804040][b])[/b][/color]
string01 [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"John-2.3.2010"[/color]
wscript[COLOR=#804040][b].[/b][/color]echo string01 [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]" -> "[/color] [COLOR=#804040][b]&[/b][/color] extract_date[COLOR=#804040][b]([/b][/color]string01[COLOR=#804040][b])[/b][/color]
string02 [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"Mary-05.01.2010"[/color]
wscript[COLOR=#804040][b].[/b][/color]echo string02 [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]" -> "[/color] [COLOR=#804040][b]&[/b][/color] extract_date[COLOR=#804040][b]([/b][/color]string02[COLOR=#804040][b])[/b][/color]
string03 [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"abc 29.6.2010"[/color]
wscript[COLOR=#804040][b].[/b][/color]echo string03 [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]" -> "[/color] [COLOR=#804040][b]&[/b][/color] extract_date[COLOR=#804040][b]([/b][/color]string01[COLOR=#804040][b])[/b][/color]
string01 [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"John-2010-03-02"[/color]
wscript[COLOR=#804040][b].[/b][/color]echo string01 [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]" -> "[/color] [COLOR=#804040][b]&[/b][/color] extract_date[COLOR=#804040][b]([/b][/color]string01[COLOR=#804040][b])[/b][/color]
string_nodate [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"abc 2010-x6-29"[/color]
wscript[COLOR=#804040][b].[/b][/color]echo string_nodate [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]" -> "[/color] [COLOR=#804040][b]&[/b][/color] extract_date[COLOR=#804040][b]([/b][/color]string_nodate[COLOR=#804040][b])[/b][/color]
[COLOR=#804040][b]function[/b][/color] extract_date[COLOR=#804040][b]([/b][/color]search_string[COLOR=#804040][b])[/b][/color]
[COLOR=#0000ff] '# extract date from string[/color]
[COLOR=#0000ff] ' create instance of RegExp [/color]
[COLOR=#804040][b]set[/b][/color] re [COLOR=#804040][b]=[/b][/color] [COLOR=#804040][b]new[/b][/color] regexp
[COLOR=#0000ff] 'or use[/color]
[COLOR=#0000ff] 'set re = createobject("vbscript.regexp")[/color]
[COLOR=#0000ff] ' regexp for matching several dates formats alternatively:[/color]
[COLOR=#0000ff] ' dd/mm/yyyy, dd.mm.yyyy, dd-mm-yyyy, yyyy-mm-dd[/color]
re[COLOR=#804040][b].[/b][/color]pattern [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"(\d{1,2}\/\d{1,2}\/\d{4})|"[/color] [COLOR=#804040][b]& _[/b][/color]
[COLOR=#ff00ff]"(\d{1,2}\.\d{1,2}\.\d{4})|"[/color] [COLOR=#804040][b]&[/b][/color] _
[COLOR=#ff00ff]"(\d{1,2}\-\d{1,2}\-\d{4})|"[/color] [COLOR=#804040][b]&[/b][/color] _
[COLOR=#ff00ff]"(\d{4})\-\d{1,2}\-\d{1,2}"[/color]
[COLOR=#0000ff] ' search for pattern[/color]
[COLOR=#804040][b]set[/b][/color] re_result [COLOR=#804040][b]=[/b][/color] re[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]execute[/color][COLOR=#804040][b]([/b][/color]search_string[COLOR=#804040][b])[/b][/color]
[COLOR=#0000ff] ' return first element of the result collection[/color]
[COLOR=#804040][b]if[/b][/color] re[COLOR=#804040][b].[/b][/color]test[COLOR=#804040][b]([/b][/color]search_string[COLOR=#804040][b])[/b][/color] [COLOR=#804040][b]then[/b][/color]
extract_date [COLOR=#804040][b]=[/b][/color] re_result[COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]0[/color][COLOR=#804040][b]).[/b][/color][COLOR=#a020f0]value[/color]
[COLOR=#804040][b]else[/b][/color]
extract_date [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"date not found!"[/color]
[COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]if[/b][/color]
[COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]function[/b][/color]
C:\Users\Roman\Work\VBscript>cscript /Nologo regex.vbs
John-2/3/2010 -> 2/3/2010
Mary-05/01/2010 -> 05/01/2010
abc xyz 29/6/2010 -> 2/3/2010
John-2.3.2010 -> 2.3.2010
Mary-05.01.2010 -> 05.01.2010
abc 29.6.2010 -> 2.3.2010
John-2010-03-02 -> 2010-03-02
abc 2010-x6-29 -> date not found!