【问题标题】:What should I change the pattern to validate?我应该改变模式来验证什么?
【发布时间】:2019-09-04 10:20:34
【问题描述】:

我必须验证电子邮件的两种类型的表达式:

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 ([.]) 合作

\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 未显示为绿色。

【问题讨论】:

  • 您的问题写得不清晰。请更好地解释您的输入内容和预期输出内容。

标签: java regex email rfc822


【解决方案1】:

下面的正则表达式与您描述的模式匹配:“letter.letter.year 大于 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.

在这里试试:https://regex101.com/r/hNIMRL/1

【讨论】:

    【解决方案2】:

    尝试以下正则表达式:“(?:[a-zA-Z]\.){2}([0-9]{4})\@uts.edu.co”

    我已针对您的示例电子邮件对其进行了测试。使用年份的捕获组,您可以验证年份是否大于 2018。RegEx 将仅匹配遵循您的 Letter.Letter.Four Digit Year@uts.edu.co 模式的电子邮件

    这是我用来测试正则表达式的.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 = {"j.g.2018@uts.edu.co","s.c.2019@uts.edu.co","t.t.2020@gmail.com"};
    
        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");
                }
            }
    
    }}
    

    从我的控制台登录:

    j.g.2018@uts.edu.co 无效

    s.c.2019@uts.edu.co 有效

    t.t.2020@gmail.com 无效

    干杯!

    【讨论】:

      【解决方案3】:

      对于大于 2018@uts.edu.co 的这种类型的电子邮件 letter.letter.year,例如 j.g.2019@uts.edu.co,您可以使用:

      ^[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 次字符 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$";
      

      Regex demo

      对于这种类型的电子邮件 0-2018.letter.letter@*.com.co,例如 0.j.g@cloudmail.com.co,您可以使用:

      ^(?: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 次点后跟字符 a-z
      • @\w+(?:\.\w+)* 匹配 @,重复 1+ 次单词字符,然后重复 0+ 次点和 1+ 单词字符
      • \.com\.co匹配.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$";
      

      Regex demo

      【讨论】:

        猜你喜欢
        • 2013-04-22
        • 2011-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-10
        • 1970-01-01
        • 1970-01-01
        • 2019-04-27
        相关资源
        最近更新 更多