1

Here is the validation that we have to use for email validation.

Using posix syntax:

^[[:graph:]]\\+@[[:graph:]]\\+\\.[[:graph:]]\\+$

I have written sample program to validate, but i have failed to give right Email address to fulfill posix syntax regular expression. Could you please any one help me out to correct my below Java example.

Java Example:

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


public class RegexMatches {

    public static void main(String args[]) {

      String email = "sudheer@gmail.com";
      String pattern ="^[[:graph:]]\\+@[[:graph:]]\\+\\.[[:graph:]]\\+$";
      //String pattern = ".+@.+\\.[a-z]+";// This is Working fine.

      if (!testRegex(email, pattern))
        System.out.println("Validator Exception");
      else
        System.out.println("Success");
    }

     public static boolean testRegex(String value, String regex) {
       Pattern pattern = Pattern.compile(regex);
       Matcher matcher = pattern.matcher(value);
       return matcher.matches();
    }
}
Bojan Petkovski
  • 6,835
  • 1
  • 25
  • 34
Sudheer
  • 23
  • 5
  • You have to use java syntax. Actually why do you think `"\\+"` is posix? This is escaping the `+` character. – akostadinov Dec 02 '14 at 12:01
  • But still this code is working fine in my system, what's the error your getting? – smali Dec 02 '14 at 12:03
  • I know this is for PHP but you should read it: http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address – m0skit0 Dec 02 '14 at 12:03

1 Answers1

2

In java you can use:

String pattern ="^\\p{Graph}+@\\p{Graph}+\\.\\p{Graph}+$";

Reference

  • Use \\p{Graph} in Java for [[:graph:]] in POSIX
  • No need to use double escape quantifier +
anubhava
  • 761,203
  • 64
  • 569
  • 643