简体   繁体   English

验证格式化的“时间”

[英]Validating a formatted “time”

I would like to validate a strictly formatted "time". 我想验证一个严格格式化的“时间”。 This "time" is of the following form: 该“时间”具有以下形式:

\d+d \d+h \d+m

where "d" represents days, "h" represents hours, and "m" represents minutes and each number has a minimum value of "1". 其中“ d”代表天,“ h”代表小时,“ m”代表分钟,每个数字的最小值为“ 1”。 The main concern is that each substring may or may not exist, but there will always be at least one. 主要问题是每个子串可能存在或可能不存在,但始终至少会有一个。 Additionally, they must be in the order specified above. 此外,它们必须按照上面指定的顺序。

Is my following RegEx the best way to validate the format? 我下面的RegEx是验证格式的最佳方法吗? Is RegEx the best balance between conciseness and speed? RegEx是简洁与速度之间的最佳平衡吗?

(?=.+)^(?:(?:[1-9]|[1-9][0-9]+)d\s?)?(?:(?:[1-9]|1[0-9]|2[0-3])h\s?)?(?:(?:[1-9]|[1-5][0-9])m)?$

I've included the lookahead to ensure an empty string does not match. 我提供了前瞻性以确保空字符串不匹配。

The only minor issue I see is that something like "23h " would match if there isn't a proceeding "m" value. 我看到的唯一一个小问题是,如果没有连续的“ m”值,则类似“ 23h”的内容将匹配。 The same goes for "d". “ d”也是如此。

Here is an optimised pattern that checks strictly the format you describe: 这是一种经过优化的模式,可以严格检查您描述的格式:

^(?=\S)(?:[1-9][0-9]*d)?(?:(?:^| )(?:1[0-9]?|[3-9]|2[0-3]?)h)?(?:(?:^| )(?:[1-5][0-9]?|[6-9])m)?$

Details: 细节:

(?=\\S) ensures that there is at least one group in the pattern and forces the string to not begin with a space. (?=\\S)确保模式中至少有一组,并强制字符串不以空格开头。 (note that you only need to find one character) (请注意,您只需找到一个字符)

[1-9][0-9]*d is faster than (?:[1-9]|[1-9][0-9]+)d since the character class [1-9] is tested only once. [1-9][0-9]*d(?:[1-9]|[1-9][0-9]+)d快,因为字符类[1-9]仅测试一次。

(?:1[0-9]?|[3-9]|2[0-3]?) a bit longer than (?:[1-9]|1[0-9]|2[0-3]) but more efficient since the first digit is tested only once. (?:1[0-9]?|[3-9]|2[0-3]?)(?:[1-9]|1[0-9]|2[0-3])但效率更高,因为第一个数字仅测试一次。 Alternatives are sorted by probability (11/22 for the first, 7/22 for the second, 4/22 for the last) 备选方案按概率排序(第一个为11/22,第二个为7/22,最后一个为4/22)

The same for [1-5][0-9]?|[6-9] . 对于[1-5][0-9]?|[6-9]

I have assumed that groups must be separated with a space. 我假设组必须用空格隔开。 To ensure that, I have removed \\s? 为了确保这一点,我已删除\\s? at the end of the groups 1 and 2 to put (?:^| ) at the begining of the groups 2 and 3. There is only two possibilities: the group is at the begining or there is an other group before and a space (remember that you can't have a space and no group before due to the lookahead) 在第1组和第2组的末尾将(?:^| )放在第2组和第3组的开始处。只有两种可能:该组在开始处,或者在前面还有另一个组和一个空格(请记住,由于前瞻,您之前不能有空间也没有群组)

You obtain in fine a pattern a little longer (one character) but faster and more binding. 您将获得更长的模式(一个字符),但速度更快且绑定更多。 If you need for an obscure reason to reduce the size of the pattern, you can gain 3 characters using the old subpatterns for numbers in group 2 and 3. 如果出于晦涩的原因需要减小图案的大小,则可以使用旧的子图案为组2和3中的数字获得3个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM