我必须验证电子邮件的两种类型的表达式:
letter.letter.year 大于 2018@uts.edu.co
j.g.2019@uts.edu.co
0-2018.letter.letter@*.com.co
0.j.g@cloudmail.com.co
我已经使用了这两个正则表达式,但它们没有起作用:
[a-zA-Z] + [.]? [a-zA-Z] + [.]? [2-9] [0-9] (?! 18 $) [1-9] [1-9] ? + $\@uts ([.]) edu ([.]) co
\b([0]|20[0-1][0-8]|2019)\b+[.]? [a-zA-Z] + [.]? [a-zA-Z] +\@ [ a-zA-Z] ([.]) com ([.]) co
private void btn_validarActionPerformed(java.awt.event.ActionEvent evt) {
String w_correo = caja_correo.getText();
Pattern p_correo1 = Pattern.compile("^[a-zA-Z]+[.]?[a-zA-Z]+[ .]?[2-9][0-9](?!18$)[1-9][1-9]?+$\\@uts([.])edu([\\.])co$");
Matcher m_correo1 = p_correo1.matcher(w_correo);
Pattern p_correo2 = Pattern.compile("^\\b([0]|20[0-1][0-8]|2019)\\b+[.]?[a-zA-Z]+[.]?[a-zA-Z]+\\@ [a-zA-Z] ([.])com([\\.])co$");
Matcher m_correo2 = p_correo2.matcher(w_correo);
if (m_correo1.matches()) {
String validacion = "";
validacion = validacion + "Direccion de correo electrónico correcta<br/>";
correcto.setForeground(Color.GREEN);
}
else {
String validacion = "";
if (!m_correo1.matches()) {
validacion= validacion + "Direccion de correo electrónico incorrecta<br/>";
incorrecto.setBackground(Color.RED);
}
}
if (m_correo2.matches()) {
String validacion = "";
validacion = validacion + "Direccion de correo electrónico correcta<br/>";
correcto.setForeground(Color.GREEN);
}
else {String validacion = "";
if (!m_correo2.matches()) {
validacion= validacion + "Direccion de correo electrónico incorrecta<br/>";
incorrecto.setBackground(Color.RED);
}
}
}
当您尝试验证有效的电子邮件时,结果是电子邮件不正确。按钮 NO 显示为红色,但按钮 SI 未显示为绿色。
最佳答案
下面的正则表达式匹配您描述的模式:“letter.letter.year greater than 2018@uts.edu.co”。
([a-zA-Z]\.){2}([2-9][1-9][0-9]{2}|20([2-9][0-9]|19))@uts\.edu\.co
([a-zA-Z]\.){2} matches any letter followed by a period two times.
([2-9][1-9][0-9]{2}|20([2-9][0-9]|19)) matches the year 2019 or greater
@uts\.edu\.co matches @uts.edu.co literally.
关于java - 我应该更改模式以验证什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55672162/