简体   繁体   English

使用正则表达式验证输入字符串是否为 0-255 之间的数字

[英]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:它适用于以下情况:

  • IP Addresses xxx.xxx.xxx.xxx / xx.xx.xx.xx / xxxx / mix of these. IP 地址 xxx.xxx.xxx.xxx / xx.xx.xx.xx / xxxx / 这些的混合。
  • Leading zeros are allowed.允许前导零。
  • Range 0-255 / maximum 3 digts.范围 0-255 / 最多 3 个数字。

You can use this regex:您可以使用此正则表达式:

boolean valid = IP.matches("\\b(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\\b");

RegEx Demo正则表达式演示

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])"

Demo on Ideone在 Ideone 上演示

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 .检查从0256的列表时的4010 步

This pattern will not match 01 or 001 .此模式将不匹配01001 (no match on one or more leading zeros) (一个或多个前导零不匹配)

Considerations:注意事项:

  1. Use quantifiers on consecutive duplicate characters.在连续的重复字符上使用量词。
  2. Organize the alternatives not in sequential order ( single-digit, double-digit, 100's, under-249, 250-255 ) but with quickest mis-matches first.不按顺序(个位数、两位数、100、249 岁以下、250-255 )组织备选方案但首先以最快的不匹配顺序排列。
  3. Avoid non-essential capture (or non-capture) groups.避免非必要的捕获(或非捕获)组。 (despite seeming logical to condense the "two hundreds" portion of the pattern) (尽管浓缩模式的“两百”部分似乎合乎逻辑)

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;
  1. (0|1)\\d{2} catches any three digit number starting with 0 or 1. (0|1)\\d{2} 捕获任何以 0 或 1 开头的三位数。
  2. 2[0-4]\\d catches numbers between 200 and 249. 2[0-4]\\d 捕获 200 到 249 之间的数字。
  3. 25[0-5] catches numbers between 250 and 255. 25[0-5] 捕获 250 到 255 之间的数字。
  4. \\d{1,2} catches any one or two digit number \\d{1,2} 捕捉任何一位或两位数

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.

相关问题 正则表达式验证输入字符串 - Regex to validate input string Java 从字符串转换为char到正确的字节0-255 - Java converting to a char from a string into the correct byte 0-255 使用setRGB设置0-255值而不是十六进制代码值 - Using setRGB to set 0-255 values instead of hex code values 使用java中的正则表达式验证用户输入字符串以匹配特定模式 - Validate a user input string to match specific pattern using regex in java 如何使用正则表达式验证字符串中两个字符之间是否没有空格 - How to validate that there are no spaces between two characters in a string, using regex 为什么ActivityCompat.requestPermissions()仅接受0-255之间的整数请求代码? - Why does ActivityCompat.requestPermissions() only accept an integer request code between 0-255? 如何将字符串(长度为1)转换为关联的ASCII代码int值(0-255)? - How to convert String (of length 1) to the associated int value of ASCII code (0-255)? 正则表达式验证两个范围之间的数字 - Regex to validate number between two ranges Java Charset支持所有符号,每个符号使用8位,每个符号的范围为[0-255] - Java Charset that supports all symbols using 8 bit per symbol from ranges [0-255] per character Java将0-255 int转换为RGB颜色 - Java convert 0-255 int to rgb color
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM