The regular expression, "String regex = "[0-9a-z]+@[0-9a-z]+.+[0-9a-z]";" , is used for testing a email validation. Basically, im trying to make it so that an email will only match this patter if the email begins with a string of alphanumeric chars, followed by 1 @ symbol, followed by another string of alphanumeric chars, followed by 1 ., and then finally a string of alphanumeric chars. Whats failing is that when i enter an email without a string of alphanumerics after the last ., the program will still match with the regex string. How do i make it so that there MUST be another string of alphanumerics after the .? The whole code is:
import java.util.Scanner;
import java.util.regex.*;
public class Regex
{
public static void main (String[]args){
Scanner input = new Scanner(System.in);
System.out.println("Please enter your Email");
String mail = input.nextLine();
String regex = "[0-9a-z]+@[0-9a-z]+.+[0-9a-z]";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(mail);
if(m.find()) {
System.out.println("VALID");
} else {
System.out.println("INVALD");
}
}
}
A slightly better regex would include start of line ^ and end of line $ anchors.
Otherwise it will only need to match a single instance of a valid email in the string and it will pass. Also, instead of the plus sign to indicate 1 or more, you could restrict it to 2 to 4 characters by adding {2,4}. Without these in place something like
will erroneously be valid.
String regex = "^[0-9a-z]+@([0-9a-z]+[.])+[0-9a-z]{2,4}$";