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验证工作完美。但我希望对通配符进行一些修改,这会导致问题。
即。我想在通配符之后通配所有输入。
正则表达式的哪些修改可以帮助我实现这一目标?有人可以帮忙吗?
答案 0 :(得分:5)
我建议如下(我在此正则表达式中使用了文字&
;当然,您可以将其更改为+ 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);
答案 1 :(得分: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 | \潮湿; |(?: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)|。&安培; |&安培;
一行中的所有内容都符合您的要求