简体   繁体   中英

What should I change the pattern to validate?

I must validate two types of expressions for an email:

letter.letter.year greater than 2018@uts.edu.co

jg2019@uts.edu.co

0-2018.letter.letter@*.com.co

0.jg@cloudmail.com.co

I have used these two regex expressions but they have not worked:

[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);
    }
}
}                                           

When you tried to validate a valid email the result is that the email is incorrect. the button NO is shown red, but the button SI is not shown green.

The regex below matches on the pattern you describe: "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.

Try it out here: https://regex101.com/r/hNIMRL/1

Try the following regex: "(?:[a-zA-Z]\\.){2}([0-9]{4})\\@uts.edu.co"

I have tested it for your example emails. Using a capture group for the year you can validate that the year is greater than 2018. The RegEx will only match emails that follow your pattern of Letter.Letter.Four Digit Year@uts.edu.co

Here is the .java I used to test the regEx:

import java.util.regex.Pattern;

public class 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");
            }
        }

}}

Log from my console:

jg2018@uts.edu.co is not valid

sc2019@uts.edu.co is valid

tt2020@gmail.com is not valid

Cheers!

For this type of e-mail letter.letter.year greater than 2018@uts.edu.co like jg2019@uts.edu.co you can use:

^[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$
  • ^ Start of string
  • [a-zA-Z]\\.[a-zA-Z]\\. Match 2 times a char az followed by a dot
  • (?:2019|20[2-9][0-9]|2[1-9][0-9]{2}|[3-9][0-9]{3}) Match range greater than 2018
  • @uts\\.edu\\.co Match @uts.edu.co
  • $ End of string

In 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

For this type of e-mail 0-2018.letter.letter@*.com.co like 0.jg@cloudmail.com.co you could use:

^(?: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$
  • ^ Start of string
  • (?:2018|201[0-7]|200[0-9]|1[0-9]{1,3}|[0-9]{1,2}) Range from 0 to 2018
  • \\.[a-zA-Z]\\.[a-zA-Z] Match 2 times a dot followed by a char az
  • @\\w+(?:\\.\\w+)* Match @, repeat 1+ times a word char followed by repeating 0+ times a dot and 1+ word chars
  • \\.com\\.co Match .com.co
  • $ End of string

In 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM