Solved

Trouble using Regex Inside VBscript

Posted on 2010-11-19
6
585 Views
Last Modified: 2012-05-10
I am trying to identify and store a date/time value in a string. This string will be read from another file. I am trying to identify the date/time and then store it in a variable using regex and vbscript. I want to identify date/time in mm/dd/yy hh:mm:ss am|pm|AM|PM.

Here is the regex that does it:
^(([0]?[1-9]|1[0-2])/([0-2]?[0-9]|3[0-1])/[0-9]\d{1})? ?((([0-1]?\d)|(2[0-3])):[0-5]\d)?(:[0-5]\d)? ?(AM|am|PM|pm)?$

Open in new window


Here is an example of one line from the document where I want to identify the date/time from:
3400,CONTROLLER IN SLOT A,6531,97.8,84.6,30893.5,30893.5,1088.5,1088.5,Date/Time: 11/12/10 2:28:52 PM

Open in new window


Here is my test code:
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Global = True
myRegExp.Pattern = "^(([0]?[1-9]|1[0-2])/([0-2]?[0-9]|3[0-1])/[0-9]\d{1})? ?((([0-1]?\d)|(2[0-3])):[0-5]\d)?(:[0-5]\d)? ?(AM|am|PM|pm)?$"
strToSearch = "3400,CONTROLLER IN SLOT A,6531,97.8,84.6,30893.5,30893.5,1088.5,1088.5,Date/Time: 11/12/10 2:28:52 PM"

Set myMatches = myRegExp.Execute(strToSearch)
For Each myMatch in myMatches
  msgbox myMatch.Value, 0, "Found Match"
Next

Open in new window

So ideally, I want to use this to look at each line in the txt document in question, and set a variable to the identified date/time value. This will then later be used for insert purposes into a sql table. I am struggling trying to get the date time value to be recognized correctly within the string. If you take off the explicit indicators (^ and $) on the regex every individual character comes up as a match but so does the exact string I am looking for. If I use the explicit indicators nothing in the string matches.

All help is welcome! Thanks in advance!
0
Comment
Question by:lffit
  • 4
  • 2
6 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Try this modification:
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Global = True
myRegExp.Pattern = "(1[012]|0?[1-9])/(3[01]|[012]\d)/\d{2} ?(1[012]|0?[1-9])(:([1-5]\d|0?\d)){2}"
strToSearch = "3400,CONTROLLER IN SLOT A,6531,97.8,84.6,30893.5,30893.5,1088.5,1088.5,Date/Time: 11/12/10 2:28:52 PM"

Set myMatches = myRegExp.Execute(strToSearch)
For Each myMatch in myMatches
  msgbox myMatch.Value, 0, "Found Match"
Next

Open in new window

0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
Perhaps that last zero shouldn't be optional  :)
(1[012]|0?[1-9])/(3[01]|[012]\d)/\d{2} ?(1[012]|0?[1-9])(:([1-5]\d|0\d)){2}

Open in new window

0
 

Author Comment

by:lffit
Comment Utility
Haha, shoot. Is that what that means? I am new to regex. I was close though right? Thanks a bunch.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
I learned from this site:  http://www.regular-expressions.info . They have a breakdown for several different languages.
0
 

Author Comment

by:lffit
Comment Utility
In case anyone is interested the exact match looks like this:
(1[012]|0?[1-9])/(3[01]|[012]\d)/\d{2} ?(1[012]|0?[1-9])(:([1-5]\d|0\d)){2}?(:[0-5]\d)? ?(AM|am|PM|pm)?

Open in new window


That matches the following format:
mm/dd/yy hh:mm:ss AM|PM|am|pm

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
I'm sorry... I completely forgot about the AM|PM part. I had it in my original attempt at your query, but then I rewrote it and forgot to put that in.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now