The best place to *find* answers to programming/development questions, imo, however it's the *worst* place to *ask* questions (if your first question/comment doesn't get any up-rating/response, then u can't ask anymore questions--ridiculously unrealistic), but again, a great reference for *finding* answers.

My Music (Nickleus)

20120806

java Pattern.compile regex with "greedy quantifier" "at least n but not more than m times" curley brackets {}


X{n,m} == "greedy quantifier"
X, at least n but not more than m times

http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

http://en.wikipedia.org/wiki/Email_address
full implementation of email regex validation: http://code.iamcal.com/php/rfc822/full_regexp.txt
practical implementation: http://www.mkyong.com/regular-expressions/how-to-validate-email-address-with-regular-expression/

so here's what i came up with:
if (!Pattern.compile("[A-Za-z0-9\\._\\-]+\\@[A-Za-z0-9\\-]+(\\.[A-Za-z0-9\\-]+)*(\\.[A-Za-z]{2,})").matcher(mail).find()){
throw new ValidatorException(new FacesMessage("Invalid email."));
}
i ended up choosing "X, at least n but not more than m times".
here are examples of valid emails checked against my regexp above:
ric-k.g.mum-phrey@g-mail.com
rick.g.mumphrey@gmail.com
rick.g.mumphrey@gmail.co.uk

No comments:

Post a Comment