package com.ssyf.common.core.util;

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

/**
 * @author: huwei
 * @date: 2020/6/30 9:38
 * @version: 1.0.0
 */
public class PatternUtil {
    /**
     * 是否为url
     * @param url
     * @return
     */
    public static boolean isUrl(String url){
        return Inner.URL_PATTERN.matcher(url).find();
    }

    /**
     * 获取url的域名
     * @param url
     * @return
     */
    public static String getUrlDomain(String url){
        Matcher matcher = Inner.URL_DOMAIN_PATTERN.matcher(url);
        if(matcher.find()){
            return matcher.group();
        }
        return null;
    }

    /**
     *
     * @param mail
     * @return
     */
    public static boolean isMail(String mail){
        return Inner.EMAIL_PATTERN.matcher(mail).find();
    }

    /**
     * 是否为手机
     * @param phone
     * @return
     */
    public static boolean isPhone(String phone){
        return Inner.PHONE_PATTERN.matcher(phone).find();
    }

    private static final class Inner {
        /**
         * url地址
         */
        private static final Pattern URL_PATTERN = Pattern.compile("^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]$");
        /**
         * url域名
         */
        private static final Pattern URL_DOMAIN_PATTERN = Pattern.compile("(http|https)://(www.)?(\\w+(\\.)?)+");
        /**
         * 手机号码
         */
        private static final Pattern PHONE_PATTERN = Pattern.compile("^1[3456789]\\d{9}$");
        /**
         * 邮箱地址
         */
        private static final Pattern EMAIL_PATTERN = Pattern.compile("[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?");
    }
}
