收集到的java 正则验证 源码

来源:互联网 发布:windows 没有ssh目录 编辑:程序博客网 时间:2024/04/29 02:23
  1. import java.util.regex.*;   
  2.   
  3. public final class RegExpValidator   
  4. {   
  5.     /**  
  6.      * 验证邮箱  
  7.      * @param 待验证的字符串  
  8.      * @return 如果是符合的字符串,返回 <b>true </b>,否则为 <b>false </b>  
  9.      */  
  10.     public static boolean isEmail(String str)   
  11.     {   
  12.         String regex = "^([//w-//.]+)@((//[[0-9]{1,3}//.[0-9]{1,3}//.[0-9]{1,3}//.)|(([//w-]+//.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(//]?)$";   
  13.         return match(regex, str);   
  14.     }   
  15.        
  16.     /**  
  17.      * 验证IP地址  
  18.      * @param 待验证的字符串  
  19.      * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>  
  20.      */  
  21.     public static boolean isIP(String str)   
  22.     {   
  23.         String num = "(25[0-5]|2[0-4]//d|[0-1]//d{2}|[1-9]?//d)";   
  24.         String regex = "^" + num + "//." + num + "//." + num + "//." + num + "$";   
  25.         return match(regex, str);   
  26.     }   
  27.   
  28.     /**  
  29.      * 验证网址Url  
  30.      * @param 待验证的字符串  
  31.      * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>  
  32.      */  
  33.     public static boolean IsUrl(String str)   
  34.     {   
  35.         String regex = "http(s)?://([//w-]+//.)+[//w-]+(/[//w- ./?%&=]*)?";   
  36.         return match(regex, str);   
  37.     }   
  38.   
  39.     /**  
  40.      * 验证电话号码  
  41.      * @param 待验证的字符串  
  42.      * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>  
  43.      */  
  44.     public static boolean IsTelephone(String str)   
  45.     {   
  46.         String regex = "^(//d{3,4}-)?//d{6,8}$";   
  47.         return match(regex, str);   
  48.     }   
  49.        
  50.     /**  
  51.      * 验证输入密码条件(字符与数据同时出现)  
  52.      * @param 待验证的字符串  
  53.      * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>  
  54.      */  
  55.     public static boolean IsPassword(String str)   
  56.     {   
  57.         String regex = "[A-Za-z]+[0-9]";   
  58.         return match(regex, str);   
  59.     }   
  60.        
  61.     /**  
  62.      * 验证输入密码长度 (6-18位)  
  63.      * @param 待验证的字符串  
  64.      * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>  
  65.      */  
  66.     public static boolean IsPasswLength(String str)   
  67.     {   
  68.         String regex = "^//d{6,18}$";   
  69.         return match(regex, str);   
  70.     }   
  71.        
  72.     /**  
  73.      * 验证输入邮政编号  
  74.      * @param 待验证的字符串  
  75.      * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>  
  76.      */  
  77.     public static boolean IsPostalcode(String str)   
  78.     {   
  79.         String regex = "^//d{6}$";   
  80.         return match(regex, str);   
  81.     }   
  82.        
  83.     /**  
  84.      * 验证输入手机号码  
  85.      * @param 待验证的字符串  
  86.      * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>  
  87.      */  
  88.     public static boolean IsHandset(String str)   
  89.     {   
  90.         String regex = "^[1]+[3,5]+//d{9}$";   
  91.         return match(regex, str);   
  92.     }   
  93.        
  94.     /**  
  95.      * 验证输入身份证号  
  96.      * @param 待验证的字符串  
  97.      * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>  
  98.      */  
  99.     public static boolean IsIDcard(String str)   
  100.     {   
  101.         String regex = "(^//d{18}$)|(^//d{15}$)";   
  102.         return match(regex, str);   
  103.     }   
  104.        
  105.     /**  
  106.      * 验证输入两位小数  
  107.      * @param 待验证的字符串  
  108.      * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>  
  109.      */  
  110.     public static boolean IsDecimal(String str)   
  111.     {   
  112.         String regex = "^[0-9]+(.[0-9]{2})?$";   
  113.         return match(regex, str);   
  114.     }   
  115.        
  116.     /**  
  117.      * 验证输入一年的12个月  
  118.      * @param 待验证的字符串  
  119.      * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>  
  120.      */  
  121.     public static boolean IsMonth(String str)   
  122.     {   
  123.         String regex = "^(0?[[1-9]|1[0-2])$";   
  124.         return match(regex, str);   
  125.     }   
  126.        
  127.     /**  
  128.      * 验证输入一个月的31天  
  129.      * @param 待验证的字符串  
  130.      * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>  
  131.      */  
  132.     public static boolean IsDay(String str)   
  133.     {   
  134.         String regex = "^((0?[1-9])|((1|2)[0-9])|30|31)$";   
  135.         return match(regex, str);   
  136.     }   
  137.            
  138.        
  139.     /**  
  140.      * 验证日期时间  
  141.      * @param 待验证的字符串  
  142.      * @return 如果是符合网址格式的字符串,返回 <b>true </b>,否则为 <b>false </b>  
  143.      */  
  144.     public static boolean isDate(String str)   
  145.     {   
  146.         //严格验证时间格式的(匹配[2002-01-31], [1997-04-30], [2004-01-01])不匹配([2002-01-32], [2003-02-29], [04-01-01])    
  147. //        String regex = "^((((19|20)(([02468][048])|([13579][26]))-02-29))|((20[0-9][0-9])|(19[0-9][0-9]))-((((0[1-9])|(1[0-2]))-((0[1-9])|(1//d)|(2[0-8])))|((((0[13578])|(1[02]))-31)|(((01,3-9])|(1[0-2]))-(29|30)))))$";   
  148.         //没加时间验证的YYYY-MM-DD   
  149. //        String regex = "^((((1[6-9]|[2-9]//d)//d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]//d|3[01]))|(((1[6-9]|[2-9]//d)//d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]//d|30))|(((1[6-9]|[2-9]//d)//d{2})-0?2-(0?[1-9]|1//d|2[0-8]))|(((1[6-9]|[2-9]//d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$";   
  150.         //加了时间验证的YYYY-MM-DD 00:00:00   
  151.         String regex = "^((((1[6-9]|[2-9]//d)//d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]//d|3[01]))|(((1[6-9]|[2-9]//d)//d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]//d|30))|(((1[6-9]|[2-9]//d)//d{2})-0?2-(0?[1-9]|1//d|2[0-8]))|(((1[6-9]|[2-9]//d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?//d):[0-5]?//d:[0-5]?//d$";   
  152.         return match(regex, str);   
  153.     }   
  154.        
  155.   
  156.     /**  
  157.      * 验证数字输入  
  158.      * @param 待验证的字符串  
  159.      * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>  
  160.      */  
  161.     public static boolean IsNumber(String str)   
  162.     {   
  163.         String regex = "^[0-9]*$";   
  164.         return match(regex, str);   
  165.     }   
  166.        
  167.     /**  
  168.      * 验证非零的正整数  
  169.      * @param 待验证的字符串  
  170.      * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>  
  171.      */  
  172.     public static boolean IsIntNumber(String str)   
  173.     {   
  174.         String regex = "^//+?[1-9][0-9]*$";   
  175.         return match(regex, str);   
  176.     }   
  177.        
  178.     /**  
  179.      * 验证大写字母  
  180.      * @param 待验证的字符串  
  181.      * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>  
  182.      */  
  183.     public static boolean IsUpChar(String str)   
  184.     {   
  185.         String regex = "^[A-Z]+$";   
  186.         return match(regex, str);   
  187.     }   
  188.   
  189.     /**  
  190.      * 验证小写字母  
  191.      * @param 待验证的字符串  
  192.      * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>  
  193.      */  
  194.     public static boolean IsLowChar(String str)   
  195.     {   
  196.         String regex = "^[a-z]+$";   
  197.         return match(regex, str);   
  198.     }   
  199.        
  200.     /**  
  201.      * 验证验证输入字母  
  202.      * @param 待验证的字符串  
  203.      * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>  
  204.      */  
  205.     public static boolean IsLetter(String str)   
  206.     {   
  207.         String regex = "^[A-Za-z]+$";   
  208.         return match(regex, str);   
  209.     }   
  210.        
  211.     /**  
  212.      * 验证验证输入汉字  
  213.      * @param 待验证的字符串  
  214.      * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>  
  215.      */  
  216.     public static boolean IsChinese(String str)   
  217.     {   
  218.         String regex = "^[/u4e00-/u9fa5],{0,}$";   
  219.         return match(regex, str);   
  220.     }   
  221.        
  222.     /**  
  223.      * 验证验证输入字符串  
  224.      * @param 待验证的字符串  
  225.      * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>  
  226.      */  
  227.     public static boolean IsLength(String str)   
  228.     {   
  229.         String regex = "^.{8,}$";   
  230.         return match(regex, str);   
  231.     }   
  232.        
  233.        
  234.     /**  
  235.      * @param regex 正则表达式字符串  
  236.      * @param str 要匹配的字符串  
  237.      * @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;  
  238.      */  
  239.     private static boolean match(String regex, String str)   
  240.     {   
  241.         Pattern pattern = Pattern.compile(regex);   
  242.         Matcher matcher = pattern.matcher(str);   
  243.         return matcher.matches();   
  244.     }   
  245.        
  246.        
  247. //    3. 检查字符串重复出现的词   
  248. //   
  249. //    private void btnWord_Click(object sender, EventArgs e)   
  250. //    {   
  251. //          System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(label1.Text,    
  252. //   
  253. //            @"/b(?<word>/w+)/s+(/k<word>)/b", System.Text.RegularExpressions.RegexOptions.Compiled |            System.Text.RegularExpressions.RegexOptions.IgnoreCase);   
  254. //           if (matches.Count != 0)   
  255. //           {   
  256. //               foreach (System.Text.RegularExpressions.Match match in matches)   
  257. //               {   
  258. //                   string word = match.Groups["word"].Value;   
  259. //                   MessageBox.Show(word.ToString(),"英文单词");   
  260. //               }   
  261. //           }   
  262. //           else { MessageBox.Show("没有重复的单词"); }   
  263. //   
  264. //   
  265. //       }    
  266. //   
  267. //4. 替换字符串   
  268. //   
  269. //  private void button1_Click(object sender, EventArgs e)   
  270. //  {   
  271. //   
  272. //           string strResult = System.Text.RegularExpressions.Regex.Replace(textBox1.Text, @"[A-Za-z]/*?", textBox2.Text);   
  273. //           MessageBox.Show("替换前字符:" + "/n" + textBox1.Text + "/n" + "替换的字符:" + "/n" + textBox2.Text + "/n" +    
  274. //   
  275. //           "替换后的字符:" + "/n" + strResult,"替换");   
  276. //   
  277. //  }   
  278. //   
  279. //5. 拆分字符串   
  280. //   
  281. // private void button1_Click(object sender, EventArgs e)   
  282. //  {   
  283. //           //实例: 甲025-8343243乙0755-2228382丙029-32983298389289328932893289丁   
  284. //           foreach (string s in System.Text.RegularExpressions.Regex.Split(textBox1.Text,@"/d{3,4}-/d*"))   
  285. //           {   
  286. //               textBox2.Text+=s; //依次输出 "甲乙丙丁"   
  287. //           }   
  288. //   
  289. //   }   
  290.   
  291.   
  292.   
  293.   
  294. }  

转载:http://hfkiss44.javaeye.com/blog/567145

原创粉丝点击