-3

I am using the email validation as mentioned below :

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

in this pattern i need (hypen, apostrophe, underscore, period) to be included.

For example - pqr.m.o'abc@xyz.com

Please suggest

Manan Kapoor
  • 675
  • 1
  • 11
  • 28
  • 1
    There is no absolute regex that can validate email addresses. The standard contains enough special cases that creating a regex is impossible. At best you can create a regex that covers the subset of addresses you want to allow – Panagiotis Kanavos Jan 22 '14 at 09:10

1 Answers1

0

You can use something like this :

^[_A-Za-z'\\.\\!'0-9-\\+]+(\\.[_A-Za-z0-9\\!\\.'-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})

REGEX: ^([_A-Za-z'\.\!'0-9-\\+]+(\.[_A-Za-z0-9\\!\\.'-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*\.[A-Za-z]{2,})$

Demo

use \. for . where \ is the escape character .

UPDATE

To remove consecutive special characters , you can use :

String ar[] ={ "pqr.m.o''abc@xyz.com","pqr.m.o'abc@xyz.com"};
String REGEX = "[_A-Za-z'\\.\\!'0-9-\\+]+(\\.[_A-Za-z0-9\\!\\.'-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})";
Pattern p = Pattern.compile(REGEX);
for(String theString:ar){
   Matcher m = p.matcher(theString);
   while (m.find()) {
        String matched = m.group();
        String regex = "([._!'-])\\1";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(matched);
        if (!matcher.find()) {
            System.out.println(matched);
        }

   }
}
Sujith PS
  • 4,776
  • 3
  • 34
  • 61
  • Have a look at here: http://en.wikipedia.org/wiki/Email_address#Valid_email_addresses There're lot of valid addresses that don't match with your regex. – Toto Jan 22 '14 at 09:38
  • The question was to update his regex , to match special characters also. – Sujith PS Jan 22 '14 at 09:44
  • @SujithPS : can u please also help me to remove two consecutive characters from above regex. like - abc..o''niel__a@xyz.com. I want to make this email address as invalidate. Please help me. – Manan Kapoor Jan 22 '14 at 12:27
  • check http://regex101.com/r/nL9yC1 – Sujith PS Jan 22 '14 at 12:28
  • Thanks for your help.. but above code will not match consecutive alphabet also. I just want to remove consecutive characters (hypen, apostrophe, underscore, period) only. Can you please tell me what needs to be changed in above code for this? – Manan Kapoor Jan 23 '14 at 05:58