Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Java Regular Expression Email Validation

Java Regular expression program to represent all valid mail ids:

[a-z][A-Z][0-9][a-z][A-Z][0-9_.]* @ [a-zA-Z0-9] + ([.][a-zA-Z]+)+

Only gmail id is:

[a-zA-Z0-9] [a-zA-Z0-9_.]* @ gmail[.]com


Java Regular expression program to check whether a given mail id is valid or invalid mail id.

import java.util.regex.*;
class RegexDemo7
{
	public static void main(String[] args)
	{
		Pattern p=Pattern.compile("[a-zA-Z0-9][a-zA-Z0-9_.]*
		@[a-zA-Z0-9]+([.][a-zA-Z]+)+");
		Matcher m=p.matcher(args[0]);
		if(m.find() && m.group().equals(args[0]))
		{
			System.out.println("valid mail id");
		}
		else
		{
			System.out.println("invalid mail id");
		}
	}
}

Output:

1st run : java RegexDemo7 trilochan4u@gmail.com
valid mail id
2nd run : java RegexDemo7 trilochan4u
invalid mail id

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext