常见正则表达式判断

import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;

public class RegexTool {
    // 日期表达式
    public static final String REG_EXT = "^([1-9]\\d{3})((0[1-9])|(1[0-2]))((0[1-9])|([1-2][0-9])|(3[0-1]))(([0-1][0-9])|(2[0-3]))([0-5][0-9])([0-5][0-9])$";
    // 手机表达式
    public static final String REG_PHONE = "^(13\\d{9})|(15[0-35-9]\\d{8})|(18[05-9]\\d{8})$";
    // 昵称表达式
    public static final String REG_NICKNAME = "^[a-zA-Z0-9]{4,16}$";
    // 邮箱表达式
    public static final String REG_EMAIL = "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$";
    // MD5值格式
    public static final String REG_MD = "^[a-zA-Z0-9]{32}$";
    // 中文真实姓名格式
    public static final String REG_REALNAME = "^[\u4E00-\u9FA5]{2,8}$";
    // 数字格式
    public static final String REG_NUMBER = "^\\d+$";
    // 时间戳格式
    public static final String REG_TIMESTAMP = "^\\d{13}$";
    // IP格式
    public static final String REG_IP = "^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$";
    // MAC地址格式
    public static final String REG_MAC = "^[0-9A-Fa-f]{2}-[0-9A-Fa-f]{2}-[0-9A-Fa-f]{2}-[0-9A-Fa-f]{2}-[0-9A-Fa-f]{2}-[0-9A-Fa-f]{2}$";

    public static boolean check(String regex, String input) {
        if (StringUtils.isNotEmpty(regex) && StringUtils.isNotEmpty(input)){
            return Pattern.matches(regex, input);
        }
        return false;
    }
}

 

posted @ 2017-06-22 10:50  十月南城  阅读(574)  评论(0编辑  收藏  举报