In day today development world, we usually required to do mandatory validation for some fields. One of the common field is email address.
The following code demonstrates how to validate email using regular expression:
public static void main(String args)
//Input the string for validation
String email = “firstname.lastname@example.org”;
//Set the email pattern string
Pattern p = Pattern.compile(“.+@.+\\.[a-z]+”);
//Match the given string with the pattern
Matcher m = p.matcher(email);
//check whether match is found
boolean matchFound = m.matches();
System.out.println(“Valid Email Id.”);
System.out.println(“Invalid Email Id.”);
Note: In my all post code snippets, used System.out.println() to print the output. Make sure that once you are fine with the coding, remove all SOPs from the file and then do the checkins. As per the SONAR code review standards this is highly blocker issue.
keep tuning to know more updates.
Have a nice day 🙂