TeddyWang 发表于 2013-2-3 11:19:48

java正则表达式举例

import java.util.regex.*;   public final class RegExpValidator   {       /**       * 验证邮箱       * @param 待验证的字符串       * @return 如果是符合的字符串,返回 <b>true </b>,否则为 <b>false </b>       */      public static boolean isEmail(String str)       {         String regex = "^([\\w-\\.]+)@((\\[{1,3}\\.{1,3}\\.{1,3}\\.)|(([\\w-]+\\.)+))({2,4}|{1,3})(\\]?)$";         return match(regex, str);       }            /**       * 验证IP地址       * @param 待验证的字符串       * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>       */      public static boolean isIP(String str)       {         String num = "(25|2\\d|\\d{2}|?\\d)";         String regex = "^" + num + "\\." + num + "\\." + num + "\\." + num + "$";         return match(regex, str);       }         /**       * 验证网址Url       * @param 待验证的字符串       * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>       */      public static boolean IsUrl(String str)       {         String regex = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";         return match(regex, str);       }         /**       * 验证电话号码       * @param 待验证的字符串       * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>       */      public static boolean IsTelephone(String str)       {         String regex = "^(\\d{3,4}-)?\\d{6,8}$";         return match(regex, str);       }            /**       * 验证输入密码条件(字符与数据同时出现)       * @param 待验证的字符串       * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>       */      public static boolean IsPassword(String str)       {         String regex = "+";         return match(regex, str);       }            /**       * 验证输入密码长度 (6-18位)       * @param 待验证的字符串       * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>       */      public static boolean IsPasswLength(String str)       {         String regex = "^\\d{6,18}$";         return match(regex, str);       }            /**       * 验证输入邮政编号       * @param 待验证的字符串       * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>       */      public static boolean IsPostalcode(String str)       {         String regex = "^\\d{6}$";         return match(regex, str);       }            /**       * 验证输入手机号码       * @param 待验证的字符串       * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>       */      public static boolean IsHandset(String str)       {         String regex = "^++\\d{9}$";         return match(regex, str);       }            /**       * 验证输入身份证号       * @param 待验证的字符串       * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>       */      public static boolean IsIDcard(String str)       {         String regex = "(^\\d{18}$)|(^\\d{15}$)";         return match(regex, str);       }            /**       * 验证输入两位小数       * @param 待验证的字符串       * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>       */      public static boolean IsDecimal(String str)       {         String regex = "^+(.{2})?$";         return match(regex, str);       }            /**       * 验证输入一年的12个月       * @param 待验证的字符串       * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>       */      public static boolean IsMonth(String str)       {         String regex = "^(0?[|1)$";         return match(regex, str);       }            /**       * 验证输入一个月的31天       * @param 待验证的字符串       * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>       */      public static boolean IsDay(String str)       {         String regex = "^((0?)|((1|2))|30|31)$";         return match(regex, str);       }                         /**       * 验证日期时间       * @param 待验证的字符串       * @return 如果是符合网址格式的字符串,返回 <b>true </b>,否则为 <b>false </b>       */      public static boolean isDate(String str)       {         //严格验证时间格式的(匹配, , )不匹配(, , )    //      String regex = "^((((19|20)(()|())-02-29))|((20)|(19))-((((0)|(1))-((0)|(1\\d)|(2)))|((((0)|(1))-31)|(((01,3-9])|(1))-(29|30)))))$";         //没加时间验证的YYYY-MM-DD   //      String regex = "^((((1|\\d)\\d{2})-(0?|1)-(0?|\\d|3))|(((1|\\d)\\d{2})-(0?|1)-(0?|\\d|30))|(((1|\\d)\\d{2})-0?2-(0?|1\\d|2))|(((1|\\d)(0||)|((16||)00))-0?2-29-))$";         //加了时间验证的YYYY-MM-DD 00:00:00         String regex = "^((((1|\\d)\\d{2})-(0?|1)-(0?|\\d|3))|(((1|\\d)\\d{2})-(0?|1)-(0?|\\d|30))|(((1|\\d)\\d{2})-0?2-(0?|1\\d|2))|(((1|\\d)(0||)|((16||)00))-0?2-29-)) (20|21|22|23|?\\d):?\\d:?\\d$";         return match(regex, str);       }                /**       * 验证数字输入       * @param 待验证的字符串       * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>       */      public static boolean IsNumber(String str)       {         String regex = "^*$";         return match(regex, str);       }            /**       * 验证非零的正整数       * @param 待验证的字符串       * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>       */      public static boolean IsIntNumber(String str)       {         String regex = "^\\+?*$";         return match(regex, str);       }            /**       * 验证大写字母       * @param 待验证的字符串       * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>       */      public static boolean IsUpChar(String str)       {         String regex = "^+$";         return match(regex, str);       }         /**       * 验证小写字母       * @param 待验证的字符串       * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>       */      public static boolean IsLowChar(String str)       {         String regex = "^+$";         return match(regex, str);       }            /**       * 验证验证输入字母       * @param 待验证的字符串       * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>       */      public static boolean IsLetter(String str)       {         String regex = "^+$";         return match(regex, str);       }            /**       * 验证验证输入汉字       * @param 待验证的字符串       * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>       */      public static boolean IsChinese(String str)       {         String regex = "^[\u4e00-\u9fa5],{0,}$";         return match(regex, str);       }            /**       * 验证验证输入字符串       * @param 待验证的字符串       * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>       */      public static boolean IsLength(String str)       {         String regex = "^.{8,}$";         return match(regex, str);       }                     /**       * @param regex 正则表达式字符串       * @param str 要匹配的字符串       * @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;       */      private static boolean match(String regex, String str)       {         Pattern pattern = Pattern.compile(regex);         Matcher matcher = pattern.matcher(str);         return matcher.matches();       }               //    3. 检查字符串重复出现的词   //   //    private void btnWord_Click(object sender, EventArgs e)   //    {   //          System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(label1.Text,    //   //            @"\b(?<word>\w+)\s+(\k<word>)\b", System.Text.RegularExpressions.RegexOptions.Compiled |            System.Text.RegularExpressions.RegexOptions.IgnoreCase);   //         if (matches.Count != 0)   //         {   //               foreach (System.Text.RegularExpressions.Match match in matches)   //               {   //                   string word = match.Groups["word"].Value;   //                   MessageBox.Show(word.ToString(),"英文单词");   //               }   //         }   //         else { MessageBox.Show("没有重复的单词"); }   //   //   //       }    //   //4. 替换字符串   //   //private void button1_Click(object sender, EventArgs e)   //{   //   //         string strResult = System.Text.RegularExpressions.Regex.Replace(textBox1.Text, @"\*?", textBox2.Text);   //         MessageBox.Show("替换前字符:" + "\n" + textBox1.Text + "\n" + "替换的字符:" + "\n" + textBox2.Text + "\n" +    //   //         "替换后的字符:" + "\n" + strResult,"替换");   //   //}   //   //5. 拆分字符串   //   // private void button1_Click(object sender, EventArgs e)   //{   //         //实例: 甲025-8343243乙0755-2228382丙029-32983298389289328932893289丁   //         foreach (string s in System.Text.RegularExpressions.Regex.Split(textBox1.Text,@"\d{3,4}-\d*"))   //         {   //               textBox2.Text+=s; //依次输出 "甲乙丙丁"   //         }   //   //   }         } 
页: [1]
查看完整版本: java正则表达式举例