`
hfkiss44
  • 浏览: 48639 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

收集到的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-\\.]+)@((\\[[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; //依次输出 "甲乙丙丁"
//           }
//
//   }




}


每天一点积累  好记性不如烂笔头
30
5
分享到:
评论
17 楼 zhang8mss 2012-11-26  
shadowsese 写道
这个东西太有帮助了,收藏啦。。。楼主辛苦了

别收 了,有错误的
16 楼 seven610 2010-01-19  
收藏了先。。。应该有用
15 楼 JackAndroid 2010-01-17  
收藏下,估计以后会有用
14 楼 anhaoy 2010-01-15  
我严重怀疑它们的正确性!
13 楼 yadsun 2010-01-14  
引用
好记性不如烂笔头
非常有同感
12 楼 wdpyyxal 2010-01-14  
多谢了!正愁呢!
11 楼 seagulls88 2010-01-14  
晕,看了功能是比较齐全了,可是后面的方法命名没按规范
10 楼 shadowsese 2010-01-13  
这个东西太有帮助了,收藏啦。。。楼主辛苦了
9 楼 xici_magic 2010-01-13  
太好了  收藏了。
8 楼 pzpyp 2010-01-13  
很经典,很少见到这么全的,赞一个!!!
7 楼 msi110 2010-01-13  
在平时的工作过程中都没有好好总结,每到用时就到处在网上搜。哎!看来我也应该好好总结一下,收藏了!3Q
6 楼 jsj05506 2010-01-12  
正在学习正则表达式...
5 楼 bbym010 2010-01-12  
很全,收藏了,谢谢
4 楼 xiaodouya 2010-01-11  
楼主辛苦,值得收藏
3 楼 terryshao 2010-01-11  
楼主辛苦啦
2 楼 汤姆猫 2010-01-11  
nypcs 写道
不错啊,收藏了,以后肯定有用,谢谢。。

1 楼 nypcs 2010-01-11  
不错啊,收藏了,以后肯定有用,谢谢。。

相关推荐

    java script 正则表达式验证大全

    正则表达式验证 java script 验证大全 部分: 下面都是我收集的一些比较常用的正则表达式,因为平常可能在表单验证的时候,用到的比较多。特发出来,让各位朋友共同使用。呵呵。 匹配中文字符的正则表达式: [u4...

    正则表达式

    收集的各种情况下的,正则验证以及一些正则表达式的使用实例

    精通正则表达式~~~

    Java的正则流派... 366 Java对\p{…}和\P{…}的支持... 369 Unicode行终结符... 370 使用java.util.regex. 371 The Pattern.compile() Factory. 372 Pattern的matcher方法... 373 Matcher对象... 373 应用...

    JS验证URL函数 正则

    搜集一个JS验证URL函数,方便需要的朋友

    java笔试题重复字符串-REGularEXpressions:所有你需要知道的关于在一个地方收集的正则表达式!

    java笔试题重复字符串 内容 一、简介 REGEX 表示 REGular EXpression,它只是字符序列。 例如,这些表达式表示 [0–9] 表示该表达式应包含数字。 正则表达式在计算机编程中的许多情况下使用。 主要在搜索、模式匹配...

    Android开发人员不得不收集的代码

    正则相关→RegularUtils.java→Test 验证手机号(简单) isMobileSimple 验证手机号(精确) isMobileExact 验证电话号码 isTel 验证身份证号码15位 isIDCard15 验证身份证号码18位 isIDCard18 验证邮箱 isEmail ...

    自己收集整理的一些常用的工具类

    RegexUtils 正则表达式工具类,提供一些常用的正则表达式 SDCardUtils SD卡工具箱 Symbols 常用符号 WebViewManager WebView管理器,提供常用设置 原地址:https://github.com/l123456789jy/Lazy

    RED HAT LINUX 6大全

    11.5.3 DNS将名字映射到IP地址及反 序操作 207 11.5.4 前区和反区必须保持同步 207 11.5.5 HUP信号和重启 207 11.5.6 IN-ADDR.ARPA域 207 11.5.7 主机命名方案 208 11.5.8 配置DNS客户:/etc/resolv.conf 208 ...

    JMeter操作手册大全.docx

    开源免费还很好用,基于Java编写,可集成到其他系统可拓展各个功能插件 支持接口测试,压力(负载和压力)测试等多种功能,支持录制回放,入门简单 相较于自己编写框架活其他开源工具,有较为完善的UI界面,便于接口...

    asp.net知识库

    SQL过程自动C#封装,支持从表到基本存储过程生成 使用SQLDMO控制 SQL Server 使用SQL-DMO实现定制SQL Scripts Create Tables and Build inserts from Tables by using Mygeneration Templates(Sql Server) C# 获取...

Global site tag (gtag.js) - Google Analytics