-1

The regular expression to validate email address using java.util.regex.Matcher is failing. It throws invalid email address or false.

How would i modify the pattern to allow email address that ends with .services

import java.util.regex.Matcher;
import java.util.regex.Pattern;

String emailAddress = myemail@something.services;

public static Pattern EMAIL_PATTERN = Pattern.compile(
    "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");

Matcher matcher = ADDR_PATTERN.matcher(emailAddress);



import java.util.regex.Matcher;
import java.util.regex.Pattern;

String emailAddress = myemail@something.services;

public static Pattern EMAIL_PATTERN = Pattern.compile(
    "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");

Matcher matcher = ADDR_PATTERN.matcher(emailAddress);
ArtuX
  • 364
  • 2
  • 11
skrk
  • 65
  • 2
  • 9

2 Answers2

3

Rather than using regex, you should use the "built-in" javax.mail.internet.InternetAddress format validator. Regex becomes more complex as you add additional rules, whereas a basic validation and then a strict validation for the suffix is human-readable.

public static boolean isValidEmailAddress(String email) {
    try {
        InternetAddress emailAddr = new InternetAddress(email);
        emailAddr.validate(); //validates email format

        return true;
    } catch (AddressException ex) {
        return false;
    }
}
Vishwa Ratna
  • 5,567
  • 5
  • 33
  • 55
Compass
  • 5,867
  • 4
  • 30
  • 42
  • It is nice way, but it may be a domain name that ends with services – taha Aug 08 '19 at 19:54
  • it only works for .services.. meaning it is stopped working for other emails like gmail, yahoo – skrk Aug 09 '19 at 19:27
  • @skrk Well, that is what you asked for, a pattern that allows ".services". If you want to allow gmail.com, yahoo.com, and so on, you wouldn't even need to check the suffix. The answer has been updated. – Compass Aug 09 '19 at 19:38
0

^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.services$

Pattern pattern = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.services$", Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher("myemail@something.services");
    System.out.println("result: "+matcher.find());

base on Java regex email

taha
  • 731
  • 2
  • 7
  • 18
  • not really working.. meaning it stopped working for other emails like gmail, yahoo – skrk Aug 09 '19 at 19:27
  • there is no exception or error thrown it return false for other emails gmail, yahoo, I want the regression that validate all the valid email address one of them is .services – skrk Aug 09 '19 at 19:56
  • you are right , I found regex for email in this website http://emailregex.com , (?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\]) – taha Aug 09 '19 at 20:53