Validate Email Address
This program validates email address using regex.
Problem Statement
Write a Java program to check if a given string is a valid email address.
Source Code
| 1 | public class RegexEmailValidation { |
| 2 | public static void main(String[] args) { |
| 3 | System.out.println("Eduinq Regex Email Validation"); |
| 4 | String email="user@eduinq.com"; |
| 5 | String regex="^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$"; |
| 6 | if(email.matches(regex)) System.out.println("Valid email"); |
| 7 | else System.out.println("Invalid email"); |
| 8 | } |
| 9 | } |
Program Output
Eduinq Regex Email Validation Valid email
Explanation
The regex checks that the string follows the general local-part@domain email format, and matches() validates the whole string against this pattern, demonstrating email validation useful for input checking.