java 使用正则表达式验证输入字符串是否为 0-255 之间的数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31684083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validate if input string is a number between 0-255 using regex
提问by user2965598
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. code is working fine but when I input 000000 up to any length is shows true instead false.
我在将输入字符串与正则表达式匹配时遇到问题。我想验证输入数字是否在 0-255 之间,并且长度最多应为 3 个字符。代码工作正常,但是当我输入 000000 到任何长度时,显示为真而不是假。
Here is my code :-
这是我的代码:-
String IP = "000000000000000";
System.out.println(IP.matches("(0*(?:[0-9][0-9]?|[0-2][0-5][0-5]))"));
回答by anubhava
You can use this regex:
您可以使用此正则表达式:
boolean valid = IP.matches("\b(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\b");
回答by Salman A
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])"
回答by Leo
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:
它适用于以下情况:
- IP Addresses xxx.xxx.xxx.xxx / xx.xx.xx.xx / x.x.x.x / mix of these.
- Leading zeros are allowed.
- Range 0-255 / maximum 3 digts.
- IP 地址 xxx.xxx.xxx.xxx / xx.xx.xx.xx / xxxx / 这些的混合。
- 允许前导零。
- 范围 0-255 / 最多 3 个数字。
回答by mickmackusa
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 stepswhen checking a list from 0
to 256
.
检查从0
到的列表时的4010 步256
。
This pattern will not match 01
or 001
. (no match on one or more leading zeros)
此模式将不匹配01
或001
。(一个或多个前导零不匹配)
Considerations:
注意事项:
- Use quantifiers on consecutive duplicate characters.
- Organize the alternatives not in sequential order (single-digit, double-digit, 100's, under-249, 250-255) but with quickest mis-matches first.
- Avoid non-essential capture (or non-capture) groups. (despite seeming logical to condense the "two hundreds" portion of the pattern)
- 在连续的重复字符上使用量词。
- 不按顺序(个位数、两位数、100、249 岁以下、250-255 岁)组织备选方案,但首先以最快的不匹配顺序排列。
- 避免非必要的捕获(或非捕获)组。(尽管浓缩模式的“两百”部分似乎合乎逻辑)
回答by Saurabh
This will work for following pattern and ip containing initial zeros e.g: 023.45.12.56
这将适用于以下包含初始零的模式和 ip,例如:023.45.12.56
pattern=(\d{1,2}|(0|1)\d{2}|2[0-4]\d|25[0-5]);
回答by RichArt
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 "A.B.C.D", where the value of A, B, C, and D may range from 0 to 255. Leading zeros are allowed. The length of A, B, C, or D can't be greater than 3.
它满足以下条件: IP 地址为“ABCD”形式的字符串,其中 A、B、C、D 的值范围为 0 到 255。允许前导零。A、B、C 或 D 的长度不能大于 3。
Maybe somebody can help with additional simplifying?
也许有人可以帮助进行额外的简化?
回答by John Guzenko
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);
}
回答by toobsco42
boolean valid = IP.matches("(0?[0-9]{1,2}|1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])");
回答by user3527035
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);
回答by BR1COP
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
它也适用于前导零