0

Looking for the regex pass the below string as a valid email:

String email = ""very.unusual.@.unusual.com"@example.com";

Yes the mail begins with " and just before the @ symbol, there's another ". Thanks

Kaleb Blue
  • 487
  • 1
  • 5
  • 20

3 Answers3

1

You could use this pattern (already Java escaped string):

"(?:[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])+)\\])"
Andie2302
  • 4,825
  • 4
  • 24
  • 43
0

well not sure how much a validation still makes sense here but this will work...

^.+@(?!-)(?:[a-zA-Z\d-]{0,62}[a-zA-Z\d].){1,126}(?!\d+)[a-zA-Z\d]{1,63}$

you can obviously change the first . do a more restrictive set of chars...

0

public class Regex_mail {

public enum Mail {

    gmail ("gmail"),
    yahoo   ("yahoo"),
    facebook   ("facebook"),
    outlook ("outlook");

    Mail(String g) {   
    }
}
/**
 * @param args the command line arguments
 */
   public static void main(String[] args){

       Mail[] m = Mail.values();
        String formatedString = Arrays.toString(m)
            .replace(",", "|")  //remove the commas
            .replace(" ", "")  //remove the right bracket
            .replace("[", "(")
            .replace("]", ")")
            .replace("", "")//remove the left bracket
            .trim();

       Scanner in = new Scanner(System.in);
        while (true) {

            Pattern pattern = Pattern.compile("[a-zA-Z0-9]"+"@"+formatedString+".com");
            Matcher matcher = 
            pattern.matcher(in.next());

            boolean found = false;
            while (matcher.find()) {
                System.out.printf("mail accepted\n",
                    matcher.group(),
                    matcher.start(),
                    matcher.end());
                found = true;
            }
            if(!found){
                System.err.println("not valid e-mail address.\n");
                //System.exit(0);
            }
        }
}

}