正则表达式验证端口号 [英] Regex to validate port number

查看:24
本文介绍了正则表达式验证端口号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这个正则表达式 (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).

这将确保任何给定的字符串都是数字并且介于 065535 之间.

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.

这篇关于正则表达式验证端口号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆