72.常用正则表达式

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-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";    
        return match(regex, str);    
    }    
        
    /**   
     * 验证IP地址   
     * @param 待验证的字符串   
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>   
     */   
    public static boolean isIP(String str)    
    {    
        String num = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\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 = "[A-Za-z]+[0-9]";    
        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 = "^[1]+[3,5]+\\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 = "^[0-9]+(.[0-9]{2})?$";    
        return match(regex, str);    
    }    
        
    /**   
     * 验证输入一年的12个月   
     * @param 待验证的字符串   
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>   
     */   
    public static boolean IsMonth(String str)    
    {    
        String regex = "^(0?[[1-9]|1[0-2])$";    
        return match(regex, str);    
    }    
        
    /**   
     * 验证输入一个月的31天   
     * @param 待验证的字符串   
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>   
     */   
    public static boolean IsDay(String str)    
    {    
        String regex = "^((0?[1-9])|((1|2)[0-9])|30|31)$";    
        return match(regex, str);    
    }    
            
        
    /**   
     * 验证日期时间   
     * @param 待验证的字符串   
     * @return 如果是符合网址格式的字符串,返回 <b>true </b>,否则为 <b>false </b>   
     */   
    public static boolean isDate(String str)    
    {    
        //严格验证时间格式的(匹配[2002-01-31], [1997-04-30], [2004-01-01])不匹配([2002-01-32], [2003-02-29], [04-01-01])     
//        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)))))$";    
        //没加时间验证的YYYY-MM-DD    
//        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-))$";    
        //加了时间验证的YYYY-MM-DD 00:00:00    
        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$";    
        return match(regex, str);    
    }    
        
   
    /**   
     * 验证数字输入   
     * @param 待验证的字符串   
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>   
     */   
    public static boolean IsNumber(String str)    
    {    
        String regex = "^[0-9]*$";    
        return match(regex, str);    
    }    
        
    /**   
     * 验证非零的正整数   
     * @param 待验证的字符串   
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>   
     */   
    public static boolean IsIntNumber(String str)    
    {    
        String regex = "^\\+?[1-9][0-9]*$";    
        return match(regex, str);    
    }    
        
    /**   
     * 验证大写字母   
     * @param 待验证的字符串   
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>   
     */   
    public static boolean IsUpChar(String str)    
    {    
        String regex = "^[A-Z]+$";    
        return match(regex, str);    
    }    
   
    /**   
     * 验证小写字母   
     * @param 待验证的字符串   
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>   
     */   
    public static boolean IsLowChar(String str)    
    {    
        String regex = "^[a-z]+$";    
        return match(regex, str);    
    }    
        
    /**   
     * 验证验证输入字母   
     * @param 待验证的字符串   
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>   
     */   
    public static boolean IsLetter(String str)    
    {    
        String regex = "^[A-Za-z]+$";    
        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, @"[A-Za-z]\*?", 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; //依次输出 "甲乙丙丁"    
//           }    
//    
//   }    
   
   
   
   
}   

        
        
[size = 12px] 1 ^ \d + $   // 匹配非负整数(正整数 + 0) 2 ^ [ 0 - 9 ] * [ 1 - 9 ][ 0 - 9 ] * $   // 匹配正整数 3 ^ (( - \d + ) | ( 0 + ))$   // 匹配非正整数(负整数 + 0) 4 ^- [ 0 - 9 ] * [ 1 - 9 ][ 0 - 9 ] * $   // 匹配负整数 5 ^-? \d + $     // 匹配整数 6 ^ \d + (\.\d + ) ? $   // 匹配非负浮点数(正浮点数 + 0) 7 ^ (([ 0 - 9 ] + \.[ 0 - 9 ] * [ 1 - 9 ][ 0 - 9 ] * ) | ([ 0 - 9 ] * [ 1 - 9 ][ 0 - 9 ] * \.[ 0 - 9 ] + ) | ([ 0 - 9 ] * [ 1 - 9 ][ 0 - 9 ] * ))$   // 匹配正浮点数 8 ^ (( - \d + (\.\d + ) ? ) | ( 0 + (\. 0 + ) ? ))$   // 匹配非正浮点数(负浮点数 + 0) 9 ^ ( - (([ 0 - 9 ] + \.[ 0 - 9 ] * [ 1 - 9 ][ 0 - 9 ] * ) | ([ 0 - 9 ] * [ 1 - 9 ][ 0 - 9 ] * \.[ 0 - 9 ] + ) | ([ 0 - 9 ] * [ 1 - 9 ][ 0 - 9 ] * )))$   // 匹配负浮点数 10 ^ ( -? \d + )(\.\d + ) ? $   // 匹配浮点数 11 ^ [A - Za - z] + $   // 匹配由26个英文字母组成的字符串 12 ^ [A - Z] + $   // 匹配由26个英文字母的大写组成的字符串 13 ^ [a - z] + $   // 匹配由26个英文字母的小写组成的字符串 14 ^ [A - Za - z0 - 9 ] + $   // 匹配由数字和26个英文字母组成的字符串 15 ^ \w + $   // 匹配由数字、26个英文字母或者下划线组成的字符串 16 ^ [\w - ] + (\.[\w - ] + ) * @[\w - ] + (\.[\w - ] + ) + $     // 匹配email地址 17 ^ [a - zA - z] + : // 匹配(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$   // 匹配url 18 。匹配中文字符的正则表达式: [\u4e00 - \u9fa5] 19 。匹配双字节字符(包括汉字在内):[ ^ \x00 - \xff] 20 。应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1) String.prototype.len = function(){ return this .replace([ ^ \x00 - \xff] / g, " aa " ).length;} 21 。匹配空行的正则表达式:\n[\s | ] * \r 22 。匹配HTML标记的正则表达式: /< (. * ) > . *< \ / \ 1 >|< (. * ) \ />/ 23 。匹配首尾空格的正则表达式:( ^ \s * ) | (\s * $) * 正则表达式用例 * 1 ^ \S + [a - z A - Z]$ 不能为空 不能有空格 只能是英文字母 * 2 、\S{ 6 ,} 不能为空 六位以上 * 3 ^ \d + $ 不能有空格 不能非数字 * 4 、(. * )(\.jpg | \.bmp)$ 只能是jpg和bmp格式 * 5 ^ \d{ 4 }\ - \d{ 1 , 2 } - \d{ 1 , 2 }$ 只能是2004 - 10 - 22格式 * 6 ^ 0 $ 至少选一项 * 7 ^ 0 { 2 ,}$ 至少选两项 * 8 ^ [\s | \S]{ 20 ,}$ 不能为空 二十字以上 * 9 ^ \ +? [a - z0 - 9 ](([ -+ .] | [_] + ) ? [a - z0 - 9 ] + ) * @([a - z0 - 9 ] + (\. | \ - )) + [a - z]{ 2 , 6 }$邮件 * 10 、\w + ([ -+ .]\w + ) * @\w + ([ - .]\w + ) * \.\w + ([ - .]\w + ) * ([,;]\s * \w + ([ -+ .]\w + ) * @\w + ([ - .]\w + ) * \.\w + ([ - .]\w + ) * ) * 输入多个地址用逗号或空格分隔邮件 * 11 ^ (\([ 0 - 9 ] + \)) ? [ 0 - 9 ]{ 7 , 8 }$电话号码7位或8位或前面有区号例如( 022 87341628 * 12 ^ [a - z A - Z 0 - 9 _] + @[a - z A - Z 0 - 9 _] + (\.[a - z A - Z 0 - 9 _] + ) + (\,[a - z A - Z 0 - 9 _] + @[a - z A - Z 0 - 9 _] + (\.[a - z A - Z 0 - 9 _] + ) + ) * $ * 只能是字母、数字、下划线;必须有@和.同时格式要规范 邮件 * 13 ^ \w + @\w + (\.\w + ) + (\,\w + @\w + (\.\w + ) + ) * $上面表达式也可以写成这样子,更精练。 14 ^ \w + (( - \w + ) | (\.\w + )) * \@\w + ((\. |- )\w + ) * \.\w + $ [ / size]
 
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值