我必须为电子邮件验证两种类型的表达式:
letter.letter.year大于[email protected]
就.个.2019@UT是.额度.co
0-2018.letter.letter@*.com.co
0.就.个@cloud mail.com.co
我使用了这两个正则表达式,但它们没有用到:
[a-zA-Z] + [。]? [a-zA-Z] + [。]? [2-9] [0-9](?!18 $)[1-9] [1-9]? + $ \ @ uts([。])edu([。])是什么
\ b([0] | 20 [0-1] [0-8] | 2019)\ b + [。]? [a-zA-Z] + [。]?每个[a-zA-Z] + \ @ [a-zA-Z]([。])com([。])
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大于[email protected]”。
([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.
请尝试以下正则表达式:“(?:[a-zA-Z] \。){2}([0-9] {4})\ @ uts.edu.co”
我已经为您的示例电子邮件测试了它。使用年度捕获组,您可以验证年份大于2018. RegEx将仅匹配遵循您的Letter.Letter.Four Digit [email protected]模式的电子邮件。
这是我用来测试regEx的.java:
import java.util.regex.Pattern;
公共类MainApp {
public static void main(String[] args) {
// reg ex to use: (?:[a-zA-Z]\.){2}([0-9]{4})\@uts.edu.co
String[] email = {"[email protected]","[email protected]","[email protected]"};
String regExPattern = "(?:[a-zA-Z]\\.){2}([0-9]{4})\\@uts.edu.co";
Pattern p = Pattern.compile( regExPattern );
for(String x : email){
Matcher m = p.matcher( x );
boolean b = m.matches();
if(b){
int year = Integer.parseInt(m.group(1));
if(year > 2018){
System.out.println(x + " is valid");
}
else{
System.out.println(x + " is not valid");
}
}
else{
System.out.println(x + " is not valid");
}
}
}}
从我的控制台登录:
干杯!
对于这种类型的电子邮件letter.year大于[email protected],如[email protected],您可以使用:
^[a-zA-Z]\.[a-zA-Z]\.(?:2019|20[2-9][0-9]|2[1-9][0-9]{2}|[3-9][0-9]{3})@uts\.edu\.co$
^
字符串的开头[a-zA-Z]\.[a-zA-Z]\.
匹配2次char a-z后跟一个点(?:2019|20[2-9][0-9]|2[1-9][0-9]{2}|[3-9][0-9]{3})
比赛范围greater比2018年@uts\.edu\.co
比赛@ uts.edu.co$
字符串结尾在Java中
String regex = "^[a-zA-Z]\\.[a-zA-Z]\\.(?:2019|20[2-9][0-9]|2[1-9][0-9]{2}|[3-9][0-9]{3})@uts\\.edu\\.co$";
对于这种类型的电子邮件0-2018.letter.letter@*.com.co,如[email protected]您可以使用:
^(?:2018|201[0-7]|200[0-9]|1[0-9]{1,3}|[0-9]{1,3})\.[a-zA-Z]\.[a-zA-Z]@\w+(?:\.\w+)*\.com\.co$
^
字符串的开头(?:2018|201[0-7]|200[0-9]|1[0-9]{1,3}|[0-9]{1,2})
范围来自0 to 2018\.[a-zA-Z]\.[a-zA-Z]
匹配2次点后跟char a-z@\w+(?:\.\w+)*
匹配@,重复单词char的1次以上,然后重复0次以上的点和1+单词字符\.com\.co
Match .com.co$
字符串结尾在Java中
String regex = "^(?:2018|201[0-7]|200[0-9]|1[0-9]{1,3}|[0-9]{1,3})\\.[a-zA-Z]\\.[a-zA-Z]@\\w+(?:\\.\\w+)*\\.com\\.co$";