正则表达式匹配捕获组中的特定数字

发布于 2025-01-13 01:16:25 字数 592 浏览 2 评论 0原文

我想了解如何实现与捕获组中的特定数字匹配的正则表达式,该数字可能后跟逗号或绝对数字。

我已经构建了这个正则表达式:

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亢潮 2025-01-20 01:16:25
(0) (\*|16) (\*) (\*) (\*|1|.*,1|1,.*|.*,1,.*)$

如果我们关注最后一部分,我们有:

\*
1
.*,1
1,.*
.*,1,.*

最后的 $ 对于前 3 种情况很重要,可以防止例如 11*1

希望这是你想要的;)

(0) (\*|16) (\*) (\*) (\*|1|.*,1|1,.*|.*,1,.*)$

if we focus on the last part, we have either:

\*
1
.*,1
1,.*
.*,1,.*

the $ at the end is important for the first 3 cases to prevent e.g. 11, *1

Hoping it's what you wanted ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文