通常我会用
进行简单的验证 private static final String EMAIL_PATTERN =
"^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)])$";
unicode标志只能来自api 24
Pattern pattern = Pattern.compile(EMAIL_PATTERN, Pattern.UNICODE_CHARACTER_CLASS);
同时在doccumentation中声明已经使用了unicode字符
/ ** *启用Unicode版本的预定义字符类和 由Unicode Technical * Standard #18: Unicode Regular Expression提供的 POSIX字符类 * 附件C:兼容性属性。 *
* *此标志对Android没有影响,unicode字符类总是如此 * 用过的。 * * @since 1.7 * /
public static final int UNICODE_CHARACTER_CLASS = 0x100;
但是当我在测试时
String[] email = new String[] {
"frakc@mail.ru", "frakc@mailru", "frakcmail.ru", "frakcru", "акфлс@ьфшд.кг", "акфлс@ьфшдкг",
"акфлсьфшд.кг", "акфлскг",
};
for (String s : email) {
Log.e("mcheck", "onCreate: " + s + " is valid " + Validator.getInstance().isValidEmail(s));
}
俄语地址未通过验证
E: onCreate: ffff@mail.ru is valid true
E: onCreate: ffffc@mailru is valid false
E: onCreate: ffffcmail.ru is valid false
E: onCreate: ffffru is valid false
E: onCreate: акфлс@ьфшд.кг is valid false
E: onCreate: акфлс@ьфшдкг is valid false
E: onCreate: акфлсьфшд.кг is valid false
E: onCreate: акфлскг is valid false