问题描述
我正在使用此正则表达式(6553 [0-5] | 655 [0-2] \d | 65 [0-4] \d {2} | 6 [0- 4] \d {3} | [1-5] \d {4} | [1-9] \d {0,3}
以验证端口号。
I'm using this regex (6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3}
to validate port numbers. Somehow this is not working. What is wrong with this? Can anybody point me out.
推荐答案
不工作到底是什么意思? ?
您可以尝试如下操作: ^([0-9] {1,4} | [1-5 ] [0-9] {4} | 6 [0-4] [0-9] {3} | 65 [0-4] [0-9] {2} | 655 [0-2] [0-9 ] | 6553 [0-5])$
(从)。
You could try something like so: ^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$
(obtained from here).
这将确保任何给定的字符串都是数字,且范围在之间0
和 65535
。
This will make sure that any given string is numeric and between the range of 0
and 65535
.
假设您的正则表达式匹配相同的范围,则为缺少开始和结束锚点(分别为 ^
和 $
),因此它将允许除实际端口以外的其他字符串。
Assuming your regular expression matches the same range, it is missing the start and end anchors (^
and $
respectively), so it would allow other strings besides the actual port.
这篇关于正则表达式以验证端口号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!