Check valid Email Id using Regex in Java
Any email id is of the form: abc.xyz@company.com or abc23.xyz@company.co.uk or abc_34year@company.com . One can validate these emails using regular expression in Java. Below is a program which can replace the email ids with a word 'EMAIL'.
package com.Nur;
public class Emailvalidation {
/**
* @param args
* @author nur.haldar
* @return
*/
public static final String EXAMPLE_TEST = "This is the emailid: nur.haldar@comviva.com" +
" from where" +
" the mail was sent to nurjamia@gmail.com. Please check " +
"the two email ids are valid or not";
public static void main(String[] args) {
//String nameRegex = "[A-Z][a-z]+\s\s?[A-Z][\.]?\s\s?[A-Z][a-z]+";
String pattern = "(\\w)(\\s+)([\\.,])";
pattern = "\\b[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}\\b";
System.out.println(EXAMPLE_TEST.replaceAll(pattern, "EMAIL"));
}
}
Output: This is the email id: EMAIL from where the mail was sent to EMAIL. Please check the two email ids are valid or not
package com.Nur;
public class Emailvalidation {
/**
* @param args
* @author nur.haldar
* @return
*/
public static final String EXAMPLE_TEST = "This is the emailid: nur.haldar@comviva.com" +
" from where" +
" the mail was sent to nurjamia@gmail.com. Please check " +
"the two email ids are valid or not";
public static void main(String[] args) {
//String nameRegex = "[A-Z][a-z]+\s\s?[A-Z][\.]?\s\s?[A-Z][a-z]+";
String pattern = "(\\w)(\\s+)([\\.,])";
pattern = "\\b[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}\\b";
System.out.println(EXAMPLE_TEST.replaceAll(pattern, "EMAIL"));
}
}
Output: This is the email id: EMAIL from where the mail was sent to EMAIL. Please check the two email ids are valid or not
Comments