Solved

Trouble using Regex Inside VBscript

Posted on 2010-11-19
6
586 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 75

Expert Comment

by:käµfm³d 👽
ID: 34175906
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 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 34175916
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
ID: 34176011
Haha, shoot. Is that what that means? I am new to regex. I was close though right? Thanks a bunch.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 75

Expert Comment

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

Author Comment

by:lffit
ID: 34176064
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 75

Expert Comment

by:käµfm³d 👽
ID: 34176144
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

832 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