package com.diagbot.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: 使用正则表达式验证输入格式
 * @author: gaodm
 * @time: 2018/9/6 13:25
 */
public class RegexValidateUtil {
    /**
     * 验证邮箱
     *
     * @param email
     * @return
     */
    public static boolean checkEmail(String email) {
        boolean flag = false;
        try {
            String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
            Pattern regex = Pattern.compile(check);
            Matcher matcher = regex.matcher(email);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    /**
     * 验证手机号码
     *
     * @param mobileNumber 手机号码
     * @return
     */
    public static boolean checkMobileNumber(String mobileNumber) {
        boolean flag = false;
        try {
            Pattern regex = Pattern.compile("^(((13[0-9])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8})|(0\\d{2}-\\d{8})|(0\\d{3}-\\d{7})$");
            Matcher matcher = regex.matcher(mobileNumber);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    /**
     * 验证密码
     * 密码必须数字和字母组成，并且要同时含有数字和字母，且长度要在6-16位之间
     * @param password 密码
     * @return
     */
    public static boolean checkPassWord(String password) {
        boolean flag = false;
        try {
            Pattern regex = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$");
            Matcher matcher = regex.matcher(password);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    public static void main(String[] args) {
        System.out.println(checkEmail("14_8@qw.df"));
        System.out.println(checkMobileNumber("139247001"));
        System.out.println(checkMobileNumber("13529600202"));
        System.out.println(checkPassWord("13529600202"));
        System.out.println(checkPassWord("12345"));
        System.out.println(checkPassWord("12345678"));
        System.out.println(checkPassWord("AAAAAAAA"));
        System.out.println(checkPassWord("1234567890abcdefg"));
        System.out.println(checkPassWord("123456AA33q"));
    }
}