使用通配符验证IP地址

时间:2022-10-20 20:23:26
String ip = "1.1.&.&";
String WILDCARD_CHARACTER = "&";
String REGEX_IP_ADDRESS_STRING = "(?:(?:"
        + WILDCARD_CHARACTER
        + "|25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:"
        + WILDCARD_CHARACTER + "|25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";
Pattern p = Pattern.compile(REGEX_IP_ADDRESS_STRING1);
Matcher m = p.matcher(ip);
System.out.println("Does it match? " + m.matches());

IP Validation with the one above coded works perfectly. But I want some modifications for wildcard character which causes problem.

使用上面编码的IP验证工作完美。但我想对通配符进行一些修改,这会导致问题。

Current scenario :

  • 192.1.&.& ------> True
  • 192.1。&。&------>真的

  • 192.1.0.1 ------> True
  • 192.1.0.1 ------>真的

  • & ------> False
  • &------>错误

  • 192.1.& ------> False
  • 192.1。&------>错误

Expected :

  • 192.1.&.& ------> False
  • 192.1。&。&------>错误

  • 192.1.0.1 ------> True
  • 192.1.0.1 ------>真的

  • & ------> True
  • &------>真的

  • 192.1.& ------> True
  • 192.1。&------>真的

i.e. I want to wildcard all the inputs after a wildcard character.

即我想在通配符之后通配所有输入。

What modifications in regular expression would help me achieve this? Can anyone please help it out?

正则表达式的哪些修改可以帮助我实现这一目标?任何人都可以帮忙吗?

2 个解决方案

#1


5  

I suggest the following (I have used a literal & in this regex; of course you can change that to your + WILDCARD_CHARACTER construct):

我建议如下(我使用了一个文字&在这个正则表达式;当然你可以改为你的+ WILDCARD_CHARACTER构造):

Pattern regex = Pattern.compile(
    "^       # Anchor the match at the start of the string\n" +
    "(?:     # Match either...\n" +
    " &      # the wildcard character\n" +
    " |      # or a number between 0 and 255\n" +
    " (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\n" +
    " \\.    # followed by a dot, followed by...\n" +
    " (?:    # ...either...\n" +
    "  &     # the wildcard character\n" +
    "  |     # or a number etc. etc.\n" +
    "  (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\n" +
    "  \\.\n" +
    "  (?:\n" +
    "   &\n" +
    "   |\n" +
    "   (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\n" +
    "   \\.\n" +
    "   (?:\n" +
    "    &\n" +
    "    |\n" +
    "    (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\n" +
    "   )\n" +
    "  )\n" +
    " )\n" +
    ")\n" +
    "$        # Anchor the match at the end of the string", 
    Pattern.COMMENTS);

#2


0  

(?:2[0-4]\d|25[0-5]|1\d\d|\d\d|\d).(?:2[0-4]\d|25[0-5]|1\d\d|\d\d|\d).(?:2[0-4]\d|25[0-5]|1\d\d|\d\d|\d).(?:2[0-4]\d|25[0-5]|1\d\d|\d\d|\d) |(?:2[0-4]\d|25[0-5]|1\d\d|\d\d|\d).(?:2[0-4]\d|25[0-5]|1\d\d|\d\d|\d).(?:2[0-4]\d|25[0-5]|1\d\d|\d\d|\d).& |(?:2[0-4]\d|25[0-5]|1\d\d|\d\d|\d).(?:2[0-4]\d|25[0-5]|1\d\d|\d\d|\d).& |(?:2[0-4]\d|25[0-5]|1\d\d|\d\d|\d).& |&

(?:2 [0-4] \ d | 25 [0-5] | 1 \ d \ d | \ d \ d | \ d)。(?: 2 [0-4] \ d | 25 [O- 5] | 1 \ d \ d | \ d \ d | \ d)(?: 2 [0-4] \ d | 25 [0-5] | 1 \ d \ d | \ d \ d | \ d )。(?:2 [0-4] \ d | 25 [0-5] | 1 \ d \ d | \ d \ d | \ d)|(?:2 [0-4] \ d | 25 [ 0-5] | 1 \ d \ d | \ d \ d | \ d)(?: 2 [0-4] \ d | 25 [0-5] | 1 \ d \ d | \ d \ d | \ d)。(?:2 [0-4] \ d | 25 [0-5] | 1 \ d \ d | \ d \ d | \ d)。&|(?:2 [0-4] \ d | 25 [0-5] | 1 \ d \ d | \ d \ d | \ d)(?: 2 [0-4] \ d | 25 [0-5] | 1 \ d \ d | \ d \ d | \ d)。&|(?:2 [0-4] \ d | 25 [0-5] | 1 \ d \ d | \ d \ d | \ d)。&|&

All on one line should match you requirements

所有在一行上应符合您的要求

#1


5  

I suggest the following (I have used a literal & in this regex; of course you can change that to your + WILDCARD_CHARACTER construct):

我建议如下(我使用了一个文字&在这个正则表达式;当然你可以改为你的+ WILDCARD_CHARACTER构造):

Pattern regex = Pattern.compile(
    "^       # Anchor the match at the start of the string\n" +
    "(?:     # Match either...\n" +
    " &      # the wildcard character\n" +
    " |      # or a number between 0 and 255\n" +
    " (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\n" +
    " \\.    # followed by a dot, followed by...\n" +
    " (?:    # ...either...\n" +
    "  &     # the wildcard character\n" +
    "  |     # or a number etc. etc.\n" +
    "  (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\n" +
    "  \\.\n" +
    "  (?:\n" +
    "   &\n" +
    "   |\n" +
    "   (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\n" +
    "   \\.\n" +
    "   (?:\n" +
    "    &\n" +
    "    |\n" +
    "    (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\n" +
    "   )\n" +
    "  )\n" +
    " )\n" +
    ")\n" +
    "$        # Anchor the match at the end of the string", 
    Pattern.COMMENTS);

#2


0  

(?:2[0-4]\d|25[0-5]|1\d\d|\d\d|\d).(?:2[0-4]\d|25[0-5]|1\d\d|\d\d|\d).(?:2[0-4]\d|25[0-5]|1\d\d|\d\d|\d).(?:2[0-4]\d|25[0-5]|1\d\d|\d\d|\d) |(?:2[0-4]\d|25[0-5]|1\d\d|\d\d|\d).(?:2[0-4]\d|25[0-5]|1\d\d|\d\d|\d).(?:2[0-4]\d|25[0-5]|1\d\d|\d\d|\d).& |(?:2[0-4]\d|25[0-5]|1\d\d|\d\d|\d).(?:2[0-4]\d|25[0-5]|1\d\d|\d\d|\d).& |(?:2[0-4]\d|25[0-5]|1\d\d|\d\d|\d).& |&

(?:2 [0-4] \ d | 25 [0-5] | 1 \ d \ d | \ d \ d | \ d)。(?: 2 [0-4] \ d | 25 [O- 5] | 1 \ d \ d | \ d \ d | \ d)(?: 2 [0-4] \ d | 25 [0-5] | 1 \ d \ d | \ d \ d | \ d )。(?:2 [0-4] \ d | 25 [0-5] | 1 \ d \ d | \ d \ d | \ d)|(?:2 [0-4] \ d | 25 [ 0-5] | 1 \ d \ d | \ d \ d | \ d)(?: 2 [0-4] \ d | 25 [0-5] | 1 \ d \ d | \ d \ d | \ d)。(?:2 [0-4] \ d | 25 [0-5] | 1 \ d \ d | \ d \ d | \ d)。&|(?:2 [0-4] \ d | 25 [0-5] | 1 \ d \ d | \ d \ d | \ d)(?: 2 [0-4] \ d | 25 [0-5] | 1 \ d \ d | \ d \ d | \ d)。&|(?:2 [0-4] \ d | 25 [0-5] | 1 \ d \ d | \ d \ d | \ d)。&|&

All on one line should match you requirements

所有在一行上应符合您的要求