尝试构建正则表达式来检查模式 - 2
我想知道是否可以添加更多检查:[之前回答的问题]( 尝试构建正则表达式来检查模式) 。
Brian Rogers 使用这个正则表达式很好地解决了上述问题:
/^([1-9]|[12][0-9]|3[01])(-([1-9]|[12][0-9]|3[01]))?(,([1-9]|[12][0-9]|3[01])(-([1-9]|[12][0-9]|3[01]))?)*$/
[作为参考,再次发布旧问题] 以
- 数字开头和结尾 连字符应以数字
- 开头和结尾 逗号应以数字
- 开头和结尾数字 数字
- 范围应为 1-31
- 如果数字以连字符 (-) 开头,则不能以逗号以外的任何其他字符结尾,并且遵循上面列出的所有规则。
例如,2-2,1
OR 2,2-1
有效,而 1-1-1-1
无效。
例如:
- 1-5,5,15-29
- 1,28,1-31,15
- 15,25,3 - 1-24,5-6,2-9
是否可以更进一步并添加其他验证?
1) 数字应按升序排列
例如:
- 1,2-3 - 有效
- 4-6,23 - 有效
- 23,4-5 - 无效
2) 数字不应重复
例如:
a) 2,2,2 - 无效
b) 2,3-6,3 - 无效
c) 2,5,7-20 - 有效
3) 如果可能
如果之前在范围内定义过,数字不应重复
例如:
a) 2,3-6, 4 - 无效,因为 4 已经是 3 到 6 之间的数字
b) 12-16, 14-18 - 无效,因为 14,15 和 16 已经在 12-16 中定义
c) 9-13、15、17-19 - 有效
I was wondering if more checks could be added: [Previously answered question](
Trying to build a regular expression to check pattern).
The above problem is brilliantly solved using this regex by Brian Rogers:
/^([1-9]|[12][0-9]|3[01])(-([1-9]|[12][0-9]|3[01]))?(,([1-9]|[12][0-9]|3[01])(-([1-9]|[12][0-9]|3[01]))?)*$/
[For Reference, posting the older problem again]
- Start and end with a number
- Hyphen should start and end with a number
- Comma should start and end with a number
- Range of number should be from 1-31
- If a number starts with a hyphen (-), it cannot end with any other character other than a comma AND follow all rules listed above.
E.g. 2-2,1
OR 2,2-1
is valid while 1-1-1-1
is not valid.
E.g.:
- 1-5,5,15-29
- 1,28,1-31,15
- 15,25,3
- 1-24,5-6,2-9
Could this go a step further and add other validations?
1) The numbers should be in ascending order
E.g:
- 1,2-3 - Valid
- 4-6,23 - Valid
- 23,4-5 - Invalid
2) The numbers should not repeat
E.g:
a) 2,2,2 - Invalid
b) 2,3-6,3 - Invalid
c) 2,5,7-20 - Valid
3) If possible
Number should not repeat if previously defined in range
E.g:
a) 2,3-6, 4 - Invalid, because 4 is already a number between 3 and 6
b) 12-16, 14-18 - Invalid, because 14,15 and 16 are already defined in 12-16
c) 9-13, 15, 17-19 - Valid
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正则表达式应该检查模式而不是处理业务逻辑。当您开始用“if ... then ... else”陈述问题时,这不是正则表达式应该处理的问题。
Regular expression is supposed to check a pattern and not to handle business logic. The moment you start stating your problem with "if ... then ... else", it's not something regular expression is supposed to handle.
正则表达式非常强大,可以用来解决您面临的挑战 - 甚至实现某种业务逻辑验证。
从架构和软件工程的角度来看,我建议您重构问题并使用程序代码来解决问题。让我解释一下原因:代码将变得
总而言之,即使正则表达式非常强大(我在我的应用程序中也大量使用它们)我不会做得太过分。它们在解决一些问题时非常优雅,但在解决简单的事情时却变得非常丑陋。
Regular expressions are very powerful and can potentially be used to solve the kind of challenge you have - even implementing some kind of business logic validation.
From an architectural and software engineering point of view, I would recommend you to restructure the problem and use rather program code to solve the issue. Let me explain why: The code will get
To summarize, even though regex are very powerful ( I am heavily using them as well in my applications) I would not overdo it. They are very elegant to solve some problems but get get very ugly to solve simple things.