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
Regex checks format of email. - matches() validates against regex. - Together demonstrates email validation. - Useful for input validation in applications. Conclusion: Shows how to validate email using regex.