java正则表达式验证IP,邮箱类

来源:互联网 发布:java io流读取txt文件 编辑:程序博客网 时间:2024/06/05 20:59
 


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class KISS {
 /**
  * 正则表达式验证IP是否规范,规范返回true,不规范则是false
  *
  * @param ip
  * @return
  */

 public boolean verifyIpStandard(String ip) {
  Pattern pattern = Pattern
    .compile("\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");
  Matcher matcher = pattern.matcher(ip);
  return matcher.matches();
 }

 /**
  * 正则表达式校验邮箱地址是否规范,规范返回true,不规范返回fale
  *
  * @param postAddress
  * @return
  */
 public boolean verifyPostBoxStandard(String postAddress) {
  Pattern pattern = Pattern
    .compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
  Matcher matcher = pattern.matcher(postAddress);
  return matcher.matches();
 }

 public static void main(String[] args) {
  KISS kiss = new KISS();
  String ip = "123.12.22.1";
  String postAddress = "woshiQQ@qq.com";
  System.out.println("邮箱地址规范" + kiss.verifyPostBoxStandard(postAddress));
  System.out.println("IP地址规范" + kiss.verifyIpStandard(ip));
 }
}

原创粉丝点击