views:

88

answers:

5

Regex needed for a time of the format: 12-02-30T00:59:43.

Also, I've not managed to find any decent websites or books that contain good explanations or reference material for regular expressions. Can anyone recommend any?

A: 

Check this site.

er4z0r
Not a fan of this website at all.
day_trader
+2  A: 

Mastering Regular Expression is a good book for regex reference. Also good sites for regex is perlretut and perlrequick. those will get you started.

As for your regex question, the simplest case can be this:

/\d{2}-\d{2}-\d{2}\w\d{2}:\d{2}:\d{2}/
ghostdog74
I shall definitely look into this book.
day_trader
A: 

You don't need a regular expression to do this--there's a python library specifically to parse date-time strings in your required format: the handy iso8601 module. Using this module versus a regular expression also saves you the date-time object conversion step):

import iso8601
tx = "2012-02-30T00:59:43"
dtm_obj = iso8601.parse_date(tx)
# returns: datetime.datetime(2012, 3, 2, 0, 59, 43, 
# tzinfo=<iso8601.iso8601.Utc object at 0x1016db150>)

But if you absolutely must use a regular expression, here it is.

a. for the date portion and "T" (year-month-day, which is what i assume the format of your example):

\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])T

b. for the time portion:

^(2[0-3]|[01]?[0-9]):([0-5]?[0-9]):([0-5]?[0-9])$

As always a few provisos:

The "T" character appears in the date regexp, to us the two regexps together, just concatenate them with no space in between them.

The date regexp will match only valid dates from Jan 1, 1900 to Dec 31 2099; however, it fails on short months (e.g., Feb 31, 2001 will match)

W/r/t the time regexp, the "?" causes leading zeros to be required for a match (e.g, 09:23:32), remove it if you want to match 9:23:32).

Again, i really try to avoid regular expressions for matching dates/times. One reason is that regular expressions are dumb when it comes to numbers, and even dumber when it comes to dates--e.g., regular expression engines don't understand that w/r/t dates "28" is always a valid day, "30" usually is, but "32" never is.

References: well, Mastering Regular Expressions is the standard source (often "standard sources" don't live up to their billing, but this one certainly does). I would also recommend Regular Expression Cookbook. An advantage of the latter is that the examples are written in 7-8 languages--the major ones certainly covered. I own both books would recommend both highly.

doug
A: 

I have been using this tool and found it really useful http://www.radsoftware.com.au/regexdesigner/

You can design and test your regex on the studio with different input string and check out the matching string. Once it is working then incorporate it to your code. This way it will save you lots of debugging time especially on heavy application that takes a long time to load.

Fadrian Sudaman
A: 

The standard library can certainly help, here: time.strptime() can extract all the information that you want (and more, generally). It's good to have it in your toolbox.

EOL