问题描述
我正在使用这个正则表达式 (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.
这篇关于正则表达式验证端口号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!