regex - SCORM 2004 时间格式 - 正则表达式?

标签 regex time scorm

我正在为 LMS 构建 SCORM 2004 javascript API,SCORM 2004 要求之一是传递给它的时间间隔必须遵循以下格式。有谁知道这个的正则表达式是什么?我试图将我的思绪环绕在它周围,但无济于事。注意:P 必须始终是第一个字符。

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


任何帮助将不胜感激。

谢谢!

更新:

我添加了一些必须遵守的额外标准——

  • 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)

最佳答案

这是我使用的正则表达式;

^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)?)?$ 

关于regex - SCORM 2004 时间格式 - 正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1307347/

相关文章:

perl - 如何在 Perl 中格式化时间戳?

javascript - 如何克服 IE8 在 SCORM 第 4 版合规性测试 SX-05 中获得 "stuck"的问题?

javascript - 句子大小写异常

java - 匹配字符串的模式

ruby - 在逗号之前匹配字符串的正则表达式,除非文本包含一个也由逗号分隔的闭合列表

ruby-on-rails - Rails 时间是否被打破为午夜?

javascript - 要检查的正则表达式 - 不超过 2 个连续数字或字符,并且不超过 1 个相同数字或字符 - Javascript

haskell - Haskell 中 zipWith fibonacci 的时间复杂度

testing - Scorm 2004 第 4 版 全部暂停 - 全部恢复

SCORM 中的性能交互