tags:

views:

212

answers:

5

I am building a SCORM 2004 javascript API for an LMS, and one of the SCORM 2004 requirements is that timeintervals passed into it must follow the following format. Does anyone know what the regular expression of this would be? I am trying to wrap my mind around it, but to no avail. Note: P must always be the first character.

P[yY][mM][dD][T[hH][nM][s[.s]S]] where:

  • y: The number of years (integer, >= 0, not restricted)
  • m: The number of months (integer, >=0, not restricted)
  • d: The number of days (integer, >=0, not restricted)
  • h: The number of hours (integer, >=0, not restricted)
  • n: The number of minutes (integer, >=0, not restricted)
  • s: The number of seconds or fraction of seconds (real or integer, >=0, not restricted). If fractions of a second are used, SCORM further restricts the string to a maximum of 2 digits (e.g., 34.45 – valid, 34.45454545 – not valid).
  • The character literals designators P, Y, M, D, T, H, M and S shall appear if the corresponding non-zero value is present.
  • Zero-padding of the values shall be supported. Zero-padding does not change the integer value of the number being represented by a set of characters. For example, PT05H is equivalent to PT5H and PT000005H.

Example -

  • P1Y3M2DT3H indicates a period of time of 1 year, 3 months, 2 days and 3 hours
  • PT3H5M indicates a period of time of 3 hours and 5 minutes

Any help would be greatly appreciated.

Thanks!

UPDATE:

I have added some additional standards that must be kept -

  • The designator P shall be present
  • If the value of years, months, days, hours, minutes or seconds is zero, the value and corresponding character literal designation may be omitted, but at least one character literal designator and value shall be present in addition to the designator P
  • The designator T shall be omitted if all of the time components (hours, minutes and seconds) are not used. A zero value may be used with any of the time components (e.g., PT0S)
A: 

Use [0-9] to match any numeral. + to match 1 or more repetitions. ? to match 0 or 1 repetitions. () to group and extract the output.

P(([0-9]+Y)?([0-9]+M)?([0-9]+D)?)(T([0-9]+H)?([0-9]+M)?([0-9.]+S)?)?

import re

>>> p = re.compile('P(([0-9]+Y)?([0-9]+M)?([0-9]+D)?)(T([0-9]+H)?([0-9]+M)?([0-9.]+S)?)?')

>>> p.match('P1Y3M2DT3H').groups()
('1Y3M2D', '1Y', '3M', '2D', 'T3H', '3H', None, None)

>>> p.match('P3M2DT3H').groups()
('3M2D', None, '3M', '2D', 'T3H', '3H', None, None)

>>> p.match('PT3H5M').groups()
('', None, None, None, 'T3H5M', '3H', '5M', None)

>>> p.match('P1Y3M4D').groups()
('1Y3M4D', '1Y', '3M', '4D', None, None, None, None)
Joe Koberg
This regex is too permissive for seconds and will match values like `'1.1.1.1.1'`.
FM
This expression works except for the detail that FM commented on. I modified it a bit to allow a 0 to be in place of 0M for example (as per the standards), so I have now: P((([0-9]+Y)|0)?(([0-9]+M)|0)?(([0-9]+D)|0)?)(T(([0-9]+H)|0)?(([0-9]+M)|0)?(([0-9.]+S)|0)?)I also found further requirements and have updated the original question.
Dan Appleyard
"0 in place of 0M"? Where's that in the spec?
Alan Moore
Similar to my comment for Alan's regex (see below), I found some examples that break. When using your regex, the following should return true but return false (apologies if I'm mistaken!): P1Y3M2D (the entire time portion may be omitted per the spec), PYMDTH23M15S (letters may be present w/o values), PYMDT2HM15.23S (letters may be present w/o values)
pipwerks
A: 

JavaScript doesn't support /x (free-spacing or comments mode), so remove the whitespace from this regex before using it.

/^P(?=.)
 (?:\d+Y)?
 (?:\d+M)?
 (?:\d+D)?
 (?:T(?=.)
    (?:\d+H)?
    (?:\d+M)?
    (?:\d+
       (?:\.\d{1,2})?
    )?
 )?$/i

Each (?=.) lookahead asserts that there's at least one character remaining at that point in the match. That means at least one of the following groups (ie, the Y, M, D or T group after the P, and the H, M or S group after the T) has to match, even though they're all optional. That satisfies the second of the added requirements in your updated spec.

Alan Moore
Thanks very much for your regex, but I found some examples that break. I believe each of the following are valid in the spec and should return true but return false (apologies if I'm mistaken!):PT00S (the entire date portion may be omitted per the spec, and zero values are acceptable for H/M/S.s)PYMDTH23M15S (letters may be present w/o numbers)PYMDT2HM15.23S (letters may be present w/o numbers)
pipwerks
A: 

Our SCORM Engine implementation uses a combination of a regular expression similar to the ones above and some basic JavaScript logic do do further validation.

Mike Rustici
A: 

Maybe it's semantics, but this part of the SCORM spec can be interpreted to mean literals are allowed even if a value isn't supplied:

The character literals designators P, Y, M, D, T, H, M and S shall appear if the corresponding non-zero value is present.

"shall appear" meaning a literal MUST be present if the corresponding number is present; it doesn't say "shall ONLY appear" if the corresponding number is present.

I modified Alan's regex to handle this possibility (thanks, Alan):

^P(?:\d+Y|Y)?(?:\d+M|M)?(?:\d+D|D)?(?:T(?:\d+H|H)?(?:\d+M|M)?(?:\d+(?:\.\d{1,2})?S|S)?)?$

The only bug I've found so far is a failure to flag a string that has no numeric values specified, such as 'PTS'. The minimum according to the spec is "P" followed by a single value and accompanying designation, such as P1Y (= 1 year) or PT0S (= 1 second):

at least one character literal designator and value shall be present in addition to the designator P

There must be a way to add a check for a numeric value to this regex, but my regex-fu is not that strong. :)

pipwerks
A: 

Hi I don't suppose if anyone has an answer for this?

pipwerks regex semi works but not sure how to implement a test for at least one numerical digit and corresponding designator.

Anyone any clues for me?

Shannow
i have done it, you need to add (?=\w*\d) which checks to make sure at least 1 alphanumeric character exists. The full regex is now; ^P(?=\w*\d)(?:\d+Y|Y)?(?:\d+M|M)?(?:\d+D|D)?(?:T(?:\d+H|H)?(?:\d+M|M)?(?:\d+(?:\.\d{1,2})?S|S)?)?$
Shannow