views:

653

answers:

4

As you know the format is like:

"YYYY-MM-DDTHH:NN:SS.ZZZ+XX:XX"  (i.e. "2009-03-24T16:24:32.057+01:00")

I have to do it in a ActionScript3 class but any source will be appreciated, thanks.

+1  A: 
\d{4}-[0-1]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-6]\d.\d{3}\+\d\d:\d\d

Or something similar?

More checks of the valid range are probably better done after the reg.ex.

Douglas Leeder
You have an small typo: it should be "\d{4}-[0-1]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-6]\d.\d{3}\+\d\d:\d\d" (slash before +)
Alekc
A: 

Regex should be something like this:

\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}\+\d{2}:\d{2}
Alekc
A: 

At the risk of being down-voted, I'd like to suggest that you don't do range-checking operations with a regex. When you find yourself having to deal with fixed format strings, it's usually easier to use string extraction (substring) from the known positions and converting relevant fields to integers for range checking.

Hence, for something like 2009-03-24T16:24:32.057+01:00:

  • get the year from positions 0-3.
  • check that position 4 is "-".
  • get the month from positions 5-6, make sure it's 01 thru 12.
  • check that position 7 is "-".
  • get the day from positions 8-9, check it against year and month.
  • check that position 10 is "T".
  • get the hour from positions 11-12, make sure it's 00 thru 23.
  • check that position 13 is ":".
  • get the minute from positions 14-15, make sure it's 00 thru 59.
  • check that position 16 is ":".
  • get the second from positions 17-18, make sure it's 00 thru 59.
  • check that position 19 is ".".
  • get the fractional part from positions 20-22, make sure it's 000 thru 999.
  • check that position 23 is "+".
  • get the hour offset from positions 24-25, make sure it's valid.
  • check that position 26 is ":".
  • get the minute offset from positions 27-28, make sure it's valid.

Now I may have gotten some of the values wrong there, I haven't checked thoroughly, but I believe that sort of code, properly documented, is more understandable than a regex that has numeric range contortions.

By all means use a regex to check that numerics and separators are in the right places, but please don't use it for things like checking that a number is between 00 and 23.

A regex that should work on just about any engine would be:

[0-9]{4}-[0-9]{2}-[0-9]{2}
    T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}
    +[0-9]{2}:[0-9]{2}

escaping the "-" and "+" characters if necessary, although you may have to use [0-9][0-9] instead of [0-9]{2} on the more simplistic engines.

paxdiablo
+1  A: 

Have a look at this related question:

Actionscript 3 - Fastest way to parse yyyy-mm-dd hh:mm:ss to a Date object?

The accepted answer provides a way to parse an UTC time string to a Date object.

Tomalak