[英]Validate if input string is a number between 0-255 using regex
I am facing problem while matching input string with Regex.我在将输入字符串与正则表达式匹配时遇到问题。 I want to validate input number is between 0-255 and length should be up to 3 characters long.
我想验证输入数字是否在 0-255 之间,并且长度最多应为 3 个字符。 code is working fine but when I input 000000 up to any length is shows true instead false.
代码工作正常,但是当我输入 000000 到任何长度时,显示为真而不是假。
Here is my code :-这是我的代码:-
String IP = "000000000000000";
System.out.println(IP.matches("(0*(?:[0-9][0-9]?|[0-2][0-5][0-5]))"));
Tested this:测试了这个:
static String pattern = "^(([0-1]?[0-9]?[0-9]?|2[0-4][0-9]|25[0-5])\\.){3}([0-1]?[0-9]?[0-9]?|2[0-4][0-9]|25[0-5]){1}$";
It works for the following:它适用于以下情况:
You can use this regex:您可以使用此正则表达式:
boolean valid = IP.matches("\\b(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\\b");
You can use this pattern which matches "0"
, "1"
, ... "255"
:您可以使用匹配
"0"
, "1"
, ... "255"
:
"([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
Using boundary tags to ensure only (0 to 255) numbers is matched, the optimized pattern that I have to offer is:使用边界标签确保只匹配(0 到 255)个数字,我必须提供的优化模式是:
\b(?:1\d{2}|2[0-4]\d|[1-9]?\d|25[0-5])\b
Pattern Demo (in PHP/PCRE to show step count)模式演示(在 PHP/PCRE 中显示步数)
4010 steps when checking a list from 0
to 256
.检查从
0
到256
的列表时的4010 步。
This pattern will not match 01
or 001
.此模式将不匹配
01
或001
。 (no match on one or more leading zeros) (一个或多个前导零不匹配)
Considerations:注意事项:
Please try this请试试这个
"^(((\d|0\d|00\d|\d{2}|0\d{2}|1\d{2}|2[0-4]\d|2[0-5]{2})\.){3})(\d|0\d|00\d|\d{2}|0\d{2}|1\d{2}|2[0-4]\d|2[0-5]{2})$"
It works also with leading zeroes它也适用于前导零
boolean valid = IP.matches("(0?[0-9]{1,2}|1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])");
Complete ip inet4 match :完成 ip inet4 匹配:
JS JS
/(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])/g.exec(myIp);
https://regex101.com/r/tU3gC3/12 https://regex101.com/r/tU3gC3/12
Minified :缩小:
/(1?(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])/g.exec(myIp);
https://regex101.com/r/tU3gC3/13 https://regex101.com/r/tU3gC3/13
这将适用于以下包含初始零的模式和 ip,例如:023.45.12.56
pattern=(\\d{1,2}|(0|1)\\d{2}|2[0-4]\\d|25[0-5]);
If you need leading zeros, try this:如果您需要前导零,请尝试以下操作:
"((\\d{1,2}|[01]\\d{1,2}|[0-2][0-4]\\d|25[0-5])\\.){3}(\\d{1,2}|[01]\\d{1,2}|[0-2][0-4]\\d|25[0-5])"
It satisfies following conditions: IP address is a string in the form "ABCD", where the value of A, B, C, and D may range from 0 to 255. Leading zeros are allowed.它满足以下条件: IP 地址为“ABCD”形式的字符串,其中 A、B、C、D 的值范围为 0 到 255。允许前导零。 The length of A, B, C, or D can't be greater than 3.
A、B、C 或 D 的长度不能大于 3。
Maybe somebody can help with additional simplifying?也许有人可以帮助进行额外的简化?
If you want to validate ip4 with 'ip/mask', so regex looks like this:如果你想用 'ip/mask' 验证 ip4,那么正则表达式看起来像这样:
^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\/([0-9]|[1-2][0-9]|3[0-2]))$
Just ip只是ip
^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
JS code to test it JS代码来测试一下
function isMatchByRegexp(stringToValidate, regexp) {
var re = new RegExp(regexp);
return re.test(stringToValidate);
}
(2[0-4][0-9])|([0-1]?[0-9]?[0-9]) (2[0-4][0-9])|([0-1]?[0-9]?[0-9])
To match 0 to 249 specifically专门匹配 0 到 249
You can simplify it by thinking in four conditions that might happen您可以通过考虑可能发生的四种情况来简化它
String zeroTo255 = "((0|1)\\d{2}|2[0-4]\\d|25[0-5]|\\d{1,2})";
String validIpPattern = zeroTo255 + "\\." + zeroTo255 + "\\." + zeroTo255 + "\\." + zeroTo255;
you can test it in https://regexr.com/您可以在https://regexr.com/ 中测试它
To test it here in regexr you need to remove one backslash要在regexr 中测试它,您需要删除一个反斜杠
(\\\\d --> \\d)
(\\\\d --> \\d)
((0|1)\d{2}|2[0-4]\d|25[0-5]|\d{1,2})
Note that \\d represents digits in regular expressions, same as [0-9]
注意\\d代表正则表达式中的数字,同[0-9]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.