<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-14274464</id><updated>2011-08-12T19:23:54.536-04:00</updated><title type='text'>Programming notes</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://programming-notes.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/14274464/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://programming-notes.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Steve Greene</name><uri>http://www.blogger.com/profile/05009463048251525338</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/-E0Fl7FoXq3k/TkW1i-wHDJI/AAAAAAAAABs/PofDk3so73Y/s220/Steve.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-14274464.post-112074782890234271</id><published>2005-07-07T10:03:00.000-04:00</published><updated>2005-08-10T15:55:56.986-04:00</updated><title type='text'>Regular expression snippet for datetime format</title><content type='html'>Note that due to line-wrapping you may miss the embedded spaces in this. So to make it explicit there's a space after the date portion (that ends with "(?:0[5-9]|[1-2][0-9])") and there's a space followed by a question mark right before the "AM/PM" stuff.&lt;br /&gt;&lt;br /&gt;Here you go, this is a long one...&lt;br /&gt;&lt;blockquote style="font-family:courier new;font-size:85%;"&gt;/^(?:0?[1-9]|1[0-2])[-\/](?:0?[1-9]|[1-2][0-9]|3[0-1])[-\/](?:20)?(?:0[5-9]|[1-2][0-9]) (?:(?:0|0?[1-9]|1[0-1]):[0-5][0-9]:[0-5][0-9] ?(?:[AP]M[ap]m))(?:(?:0|0?[1-9]|1[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9])$/&lt;/blockquote&gt;&lt;br /&gt;If you're wondering what the "?:" is all about, it's a notation to the regex engine telling it to not worry about keeping track of any backreferences (which speeds things up). So "(?:&lt;em&gt;expression&lt;/em&gt;)" means that the captured expression won't go into any \1 or $1 reference (if you don't need it), and the regex evaluation will run faster.&lt;br /&gt;&lt;br /&gt;In Perl for example you'd use this expression to validate a variable holding a datetime value (making sure the variable is left- and right-trimmed of whitespace; or you can modify the regular expression to account for possible left- or right-whitespace), like this:&lt;br /&gt;&lt;blockquote style="font-family:courier new;font-size:85%;"&gt;if ($dt_str !~ m/^{&lt;em&gt;mess&lt;/em&gt;}$/)&lt;/blockquote&gt;&lt;br /&gt;which means you're checking to see if the "$dt_str" variable value does not match the "{mess}" pattern (where {mess} is the long regular expression above).&lt;br /&gt;&lt;br /&gt;Note that the year part as I've written it here validates for years from 2005 to 2029 (and the "20" is optional) which fits what I need it to do, so you may need to change the years being checked for your own needs. As I've written it it allows for a date format like "mm-dd/yy" or "mm/dd-yy" but to force consistency in the delimiters would have made it just so much longer and I didn't feel like that one was very important, because I can still get the relevant data from that. In Perl, you could force consistency on the delimiters like this&lt;br /&gt;&lt;blockquote style="font-family:courier new;font-size:85%;"&gt;$dt_str =~ s/^(\d?\d)[-\/](\d?\d)[-\/]((?:\d\d)?\d\d)/\1-\2-\3/;&lt;/blockquote&gt;&lt;br /&gt;which forces the date delimiters to be dashes. Note also that the expression here allows for either AM/PM format or for military time (00:00:00 to 23:59:59), and it allows for upper- or lowercase AM/PM.&lt;br /&gt;&lt;br /&gt;I hope this is useful to you.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/14274464-112074782890234271?l=programming-notes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-notes.blogspot.com/feeds/112074782890234271/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=14274464&amp;postID=112074782890234271' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/14274464/posts/default/112074782890234271'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/14274464/posts/default/112074782890234271'/><link rel='alternate' type='text/html' href='http://programming-notes.blogspot.com/2005/07/regular-expression-snippet-for.html' title='Regular expression snippet for datetime format'/><author><name>Steve Greene</name><uri>http://www.blogger.com/profile/05009463048251525338</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/-E0Fl7FoXq3k/TkW1i-wHDJI/AAAAAAAAABs/PofDk3so73Y/s220/Steve.jpg'/></author><thr:total>0</thr:total></entry></feed>
