English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
To validate whether the given input string is a valid email ID, please use the following regular expression to match the given input string to match the email ID-
"^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+$"
Where,
^ matches the beginning of the sentence.
[a-zA-Z0-9 + _.-] matches a character from the English alphabet (two cases), numbers "+", "_", ".", before the @ symbol.
+ means repeating the above character set once or more.
@ matches itself.
[a-zA-Z0-9.-] matches a character from the English alphabet (two cases), numbers ".", at the "-" after the @ symbol.
$ represents the end of the sentence.
import java.util.Scanner; public class ValidatingEmail { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter your Email: "); String phone = sc.next(); String regex = "^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+$"; //Match the given number with the regular expression boolean result = phone.matches(regex); if(result) { System.out.println("Given email-id is valid"); } else { System.out.println("Given email-id is not valid"); } } }
Enter your Email: [email protected] Given email-id is valid
Enter your Email: [email protected] Given email-id is not valid
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter your name:"); String name = sc.nextLine(); System.out.println("Enter your email id:"); String phone = sc.next(); //Regular expression to accept valid email ID String regex = "^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+$"; //Create a pattern object Pattern pattern = Pattern.compile(regex); //Create a Matcher object Matcher matcher = pattern.matcher(phone); //Validate if the given number is valid if(matcher.matches()) { System.out.println("Given email id is valid"); } else { System.out.println("Given email id is not valid"); } } }
Enter your name: vagdevi Enter your email id: [email protected] Given email id is valid
Enter your name: raja Enter your email id: [email protected] Given email id is not valid