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

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

问题描述

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

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

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