regex 正则表达式

来源:互联网 发布:mysql持久化 编辑:程序博客网 时间:2024/06/11 21:56

regex 正则表达式


首先贴出两个网址:
第一个《正则表达式30分钟入门教程》
http://deerchao.net/tutorials/regex/regex.htm
第二个 在线正则测试网站
http://tool.oschina.net/regex/

what is regex?

正则表达式就是 字符串规则
使用正则表达式,就是
1.为了找到符合规则的字符串,例如爬虫中的应用;
2.为了验证字符串是否符合规则,例如用户表单校验。

base Content

^匹配你要用来查找的字符串的开头,$匹配结尾。
javascript 中使用要使用 // 对表达式进行包裹,java 中直接使用即可。
javascript 使用例子:

/^[a-zA-Z0-9]{6,20}$/

project example

以下内容为自己项目中使用过的正则表达式
java中应用

    /**     *      * @Description:手机号码校验     * @author:      * @time:2016年12月14日 下午12:41:41     */    public static boolean telephoneNumCheck(String telephoneNum) {        Pattern p = Pattern                .compile("^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\\d{8})$");        Matcher m = p.matcher(telephoneNum);        return m.matches();    }    /**     *      * @Description:身份证号码校验     * @author:      * @time:2016年12月14日 下午12:43:05     */    public static boolean idCardNumCheck(String idCardNum) {        Pattern p = Pattern                .compile("^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$");        Matcher m = p.matcher(idCardNum);        if (m.matches()) {            if (idCardNum.length() == 18) {                Integer[] idCardWi = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10,                        5, 8, 4, 2 };                // 这是除以11后,可能产生的11位余数、验证码,也保存成数组                Integer[] idCardY = { 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 };                // 用来保存前17位各自乖以加权因子后的总和                Integer idCardWiSum = 0;                for (int i = 0; i < 17; i++) {                    idCardWiSum += Integer.parseInt(idCardNum.substring(i,                            i + 1)) * idCardWi[i];                }                int idCardMod = idCardWiSum % 11;// 计算出校验码所在数组的位置                String idCardLast = idCardNum.substring(17);// 得到最后一位身份证号码                if (idCardMod == 2) {                    if ("X".equals(idCardLast) || "x".equals(idCardLast)) {                        return true;                    } else {                        return false;                    }                } else {                    // 用计算出的验证码与最后一位身份证号码匹配,如果一致,说明通过,否则是无效的身份证号码                    if (Integer.parseInt(idCardLast) == idCardY[idCardMod]) {                        return true;                    } else {                        return false;                    }                }            }else{                return true;            }        } else {            return false;        }    }    /**     *      * @Description:邮编校验     * @author:      * @time:2016年12月14日 下午2:01:21     */    public static boolean zipCodeCheck(String zipCode) {        Pattern p = Pattern.compile("^[1-9]\\d{5}$");        Matcher m = p.matcher(zipCode);        return m.matches();    }    /**     *      * @Description:身份证姓名校验     * @author:      * @time:2016年12月14日 下午2:23:49     */    public static boolean nameCheck(String name) {        Pattern p = Pattern                .compile("[\\u4E00-\\u9FA5]{2,5}(?:·[\\u4E00-\\u9FA5]{2,5})*");        Matcher m = p.matcher(name);        return m.matches();    }    /**     * 过滤特殊字符     * @param str     * @return     */    public static String  specialCharacterFilter(String str){        String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";        Pattern p = Pattern.compile(regEx);        Matcher m = p.matcher(str);        return m.replaceAll("").trim();     }

javascript 应用

/**校验手机号*/$.validator.addMethod("mobilePhone",function(value){    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/;    if(myreg.test(value)){        return true;    }else {        return false;    }},"请输入正确的手机号码");/**校验长度*/$.validator.addMethod("checkNameLen", function(value) {    if(/^[\u4e00-\u9fa5a-zA-Z0-9]{2,20}$/.test(value)) {        return true;    }        return false;}, "请输入2-20个汉字或2-20位字符,不包含特殊字符");/**校验长度*/$.validator.addMethod("checkUnameLen", function(value) {    if(/^[a-zA-Z0-9]{6,20}$/.test(value)){        return true;    }    return false;}, "请输入6-20位字符,不包含汉字及特殊字符");/**密码校验**/$.validator.addMethod("checkPassword", function(value) {    if(/^[a-zA-Z0-9_]{6,20}$/.test(value)){        return true;    }    return false;}, "请输入6-20位数字、字母或下划线");/** * 校验全局唯一用户名 */$.validator.addMethod("primaryUname", function(value, element) {    var succ = false;    if($("#customerId").val()!=""){        succ = true;    }    var username=$("#uname").val();    if(username == value){        succ=true    }else{    $.ajax({        type : "POST",        url : unameCkeckPath,        data : {nickName : value},        async : false,        success : function(msg) {            if (msg == "succ") {                succ = true            }        }    });}    return succ;}, "账户名已存在");$.validator.addMethod("pwdEqUname", function(value) {    var username = $("#userName").val();    if (value != username) {        return true;    } else {        return false;    }}, "密码不能和用户名一样");//检查第二次输入密码是否与上面的一样$.validator.addMethod("repassEqual", function(value) {    var pwd1 = $("#loginPassword").val();    var pwd2 = value;    if (pwd1==pwd2) {        return true;    } else {        return false;    }}, "两次输入密码不一样");// 检查邮箱,为空不检查$.validator.addMethod("email", function(value) {    var isNul = false;    if(!value){isNul = true}    return isNul || /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(value);}, "请输入正确的邮箱");
0 0
原创粉丝点击