【发布时间】:2015-06-20 13:33:17
【问题描述】:
我写了下面的正则表达式
(1[012]|[1-9])(am|pm)\-(1[012]|[1-9])(am|pm)
匹配以下时间格式:
7am-10pm (matches correctly and creates 4 match groups 7, am, 10, pm)
13am-10pm (this should not be matched, however it matches and creates 4 match groups 3, am, 10, pm)
10pm (this doesn't match as expected because it doesn't specify the time range end)
111am-10pm (this should not be matched, however it matches and creates 4 match groups 11, am, 10, pm)
如何改进我的正则表达式,这样我就不需要重复数字和 am/pm 模式以及以下内容:
它仅捕获时间范围组件,例如上午 7 点至上午 10 点应该只有 2 个匹配组,上午 7 点、上午 10 点。
它只匹配正确的时间,例如上午 111 点或下午 13 点等应被视为不匹配。
我不知道是否可以使用正则表达式,但我们可以让正则表达式匹配正确的时间范围,例如7am-1pm 应该匹配,但是 4pm-1pm 应该被视为不匹配?
注意:我使用的是 Ruby 2.2.1
谢谢。
【问题讨论】:
标签: regex time-format ruby-2.2