Java 用正则表达式判断是否为IP

来源:互联网 发布:网络图片 编辑:程序博客网 时间:2024/06/14 00:04
  1. 代码
/**  1. @Title:IpAddress.java  2. @Package:com.you.dao  3. @Description:用正则表达式判断是否为IP  5. @date: 2014年3月4日 下午10:55:06  6. @Version V1.2.3  */  package com.you.dao;  import java.util.regex.Matcher;  import java.util.regex.Pattern;  /**  7. @类名:IpAddress  8. @描述:用正则表达式判断是否为IP  9. @Author:Administrator  10. @date: 2014年3月4日 下午10:55:06  */  public class IpAddress   {      public static class IpAdd      {          public boolean isIP(String addr)          {              if(addr.length() < 7 || addr.length() > 15 || "".equals(addr))              {                  return false;              }              /**              * 判断IP格式和范围              */              String rexp = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";              Pattern pat = Pattern.compile(rexp);                Matcher mat = pat.matcher(addr);                boolean ipAddress = mat.find();              return ipAddress;          }      }      /**      * @Title : main      * @Type : IpAddress      * @date : 2014年3月4日 下午10:55:06      * @Description : IP可能的范围是0-255.0-255.0-255.0-255      * @param args      */      public static void main(String[] args)       {          /**          * 符合IP地址的范围          */           String oneAddress = "10.127.30.45";           /**          * 符合IP地址的长度范围但是不符合格式          */           String twoAddress = "127.30.45";           /**          * 不符合IP地址的长度范围          */           String threeAddress = "7.0.4";           /**          * 不符合IP地址的长度范围但是不符合IP取值范围          */           String fourAddress = "255.255.255.2567";           IpAdd ipAdd = new IpAdd();           //判断oneAddress是否是IP           System.out.println(ipAdd.isIP(oneAddress));           //判断twoAddress是否是IP           System.out.println(ipAdd.isIP(twoAddress));           //判断threeAddress是否是IP           System.out.println(ipAdd.isIP(threeAddress));           //判断fourAddress是否是IP           System.out.println(ipAdd.isIP(fourAddress));      }  } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  1. 运行结果

true 
false 
false 
false

附录

  1. find()方法是部分匹配,是查找输入串中与模式匹配的子串,如果该匹配的串有组还可以使用group()函数。
  2. matches()是全部匹配,是将整个输入串与模式匹配,如果要验证一个输入的数据是否为数字类型或其他类型,一般要用matches()。 
    Pattern pattern= Pattern.compile(“.?,(.)”);

    Matcher matcher = pattern.matcher(result);

    if (matcher.find()) { 
    return matcher.group(1); 
    }

  3. 详解:

matches 
public static boolean matches(String regex, CharSequence input)

编译给定正则表达式并尝试将给定输入与其匹配。 
调用此便捷方法的形式 
Pattern.matches(regex, input); 
Pattern.compile(regex).matcher(input).matches() ; 
如果要多次使用一种模式,编译一次后重用此模式比每次都调用此方法效率更高。 
参数: 
regex - 要编译的表达式 
input - 要匹配的字符序列 
抛出: 
PatternSyntaxException - 如果表达式的语法无效

find 
public boolean find()尝试查找与该模式匹配的输入序列的下一个子序列。 
此方法从匹配器区域的开头开始,如果该方法的前一次调用成功了并且从那时开始匹配器没有被重置,则从以前匹配操作没有匹配的第一个字符开始。 
如果匹配成功,则可以通过 start、end 和 group 方法获取更多信息。

matcher.start() 返回匹配到的子字符串在字符串中的索引位置. 
matcher.end()返回匹配到的子字符串的最后一个字符在字符串中的索引位置. 
matcher.group()返回匹配到的子字符串 
返回: 
当且仅当输入序列的子序列匹配此匹配器的模式时才返回 true。

4.部分JAVA正则表达式实例

①字符匹配 
Pattern p = Pattern.compile(expression); // 正则表达式 
Matcher m = p.matcher(str); // 操作的字符串 
boolean b = m.matches(); //返回是否匹配的结果 
System.out.println(b);

Pattern p = Pattern.compile(expression); // 正则表达式 
Matcher m = p.matcher(str); // 操作的字符串 
boolean b = m. lookingAt (); //返回是否匹配的结果 
System.out.println(b);

Pattern p = Pattern.compile(expression); // 正则表达式 
Matcher m = p.matcher(str); // 操作的字符串 
boolean b = m..find (); //返回是否匹配的结果 
System.out.println(b);

②分割字符串 
Pattern pattern = Pattern.compile(expression); //正则表达式 
String[] strs = pattern.split(str); //操作字符串 得到返回的字符串数组

③替换字符串 
Pattern p = Pattern.compile(expression); // 正则表达式 
Matcher m = p.matcher(text); // 操作的字符串 
String s = m.replaceAll(str); //替换后的字符串

④查找替换指定字符串 
Pattern p = Pattern.compile(expression); // 正则表达式 
Matcher m = p.matcher(text); // 操作的字符串 
StringBuffer sb = new StringBuffer(); 
int i = 0; 
while (m.find()) { 
m.appendReplacement(sb, str); 
i++; //字符串出现次数 

m.appendTail(sb);//从截取点将后面的字符串接上 
String s = sb.toString(); 
⑤查找输出字符串 
Pattern p = Pattern.compile(expression); // 正则表达式 
Matcher m = p.matcher(text); // 操作的字符串 
while (m.find()) { 
matcher.start() ; 
matcher.end(); 
matcher.group(1); 
}

0 0