2

I must validate two types of expressions for an email:

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

j.g.2019@uts.edu.co

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

0.j.g@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.

  • Your question isn't legible as written. Please explain better what you have on input and what is expected of output. – mvp Apr 14 '19 at 06:42

3 Answers3

0

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

Vqf5mG96cSTT
  • 2,561
  • 3
  • 22
  • 41
0

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:

j.g.2018@uts.edu.co is not valid

s.c.2019@uts.edu.co is valid

t.t.2020@gmail.com is not valid

Cheers!

snekcode
  • 91
  • 4
0

For this type of e-mail letter.letter.year greater than 2018@uts.edu.co like j.g.2019@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 a-z 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.j.g@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 a-z
  • @\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 fourth bird
  • 154,723
  • 16
  • 55
  • 70