正则表达式匹配捕获组中的特定数字
我想了解如何实现与捕获组中的特定数字匹配的正则表达式,该数字可能后跟逗号或绝对数字。
我已经构建了这个正则表达式:
(0) (\*|16) (\*) (\*) (\*|1|(,[1]))
此正则表达式匹配一些无效的模式,因为我想确保字符串的第五组上有任何数字 1 。
示例:
0 16 * * 4,6,1 # Valid
0 16 * * 4,1,6 # Valid
0 * * * 3,1 # Valid
0 16 * * 6 # Invalid
0 * * * * # Valid
0 16 * * 1,3,6 # Valid
0 16 * * 1 # Valid
0 16 * * 11 # Invalid
到目前为止,匹配的内容是:
0 * * * * # Correct
0 16 * * 1,3,6 # Correct
0 16 * * 1 # Correct
0 16 * * 11 # Wrong
有什么办法可以解决这个问题吗?谢谢!
I would like to understand how I can implement a Regex that matches a specific number in a capturing group which may be followed by a comma or be the absolute number.
I have built this Regex:
(0) (\*|16) (\*) (\*) (\*|1|(,[1]))
This Regex matches some patterns which aren't valid since I want to make sure I have any number 1 on the fifth group of the string.
Examples:
0 16 * * 4,6,1 # Valid
0 16 * * 4,1,6 # Valid
0 * * * 3,1 # Valid
0 16 * * 6 # Invalid
0 * * * * # Valid
0 16 * * 1,3,6 # Valid
0 16 * * 1 # Valid
0 16 * * 11 # Invalid
So far the ones matched are:
0 * * * * # Correct
0 16 * * 1,3,6 # Correct
0 16 * * 1 # Correct
0 16 * * 11 # Wrong
Any way I can get around with this? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我们关注最后一部分,我们有:
最后的
$
对于前 3 种情况很重要,可以防止例如11
、*1
希望这是你想要的;)
if we focus on the last part, we have either:
the
$
at the end is important for the first 3 cases to prevent e.g.11
,*1
Hoping it's what you wanted ;)