需求

  1. 判断邮箱地址是否合法;
  2. 批量判断并过滤合法地址列表。

代码模块

public static List<String> getValidEmailAddressList(List<String> addressList) {
    	//新地址列表返回
        List<String> newEmailAddressList = new ArrayList<>();
        // 非空判断
        if (CollectionUtil.isEmpty(addressList)) {
            return newEmailAddressList;
        }
        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 = null;
            for (String str : addressList) {
                matcher = regex.matcher(str);
                // 匹配
                if (matcher.matches()) {
                    newEmailAddressList.add(str);
                }
            }
        } catch (Exception e) {
            log.error("email address check error: {}", e.getMessage());
        }
        return newEmailAddressList;
    }