package com.youchain;

import java.util.regex.Pattern;

public class EmailValidator {
    // 正则表达式规则来源于：https://howtodoinjava.com/java/regex/java-email-address-validation-regex/
    private static final String EMAIL_PATTERN =
            "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

    private static final Pattern pattern = Pattern.compile(EMAIL_PATTERN);

    public static boolean validate(String email) {
        if (email == null) {
            return false;
        }
        return pattern.matcher(email).matches();
    }

    public static void main(String[] args) {
        // 测试电子邮件格式是否正确
        System.out.println(validate("houjl@youchain56.com")); // true
        System.out.println(validate("example.test@sub.test.com")); // true
        System.out.println(validate("example.test.com")); // false
        System.out.println(validate("example#test@test.com")); // false
    }
}