regex - Java - Regular Expression -
i have regular expression validate email:
validemail = ^[^\\^~`'!@$#=%&*()+|{}:;,><?\"\\/\\[\\]\\\\\\s-\\.]([^\\^~`'!@$#=%&*()+|{}:;,><?\"\\/\\[\\]\\\\\\s\\.]|\\.(?!\\.+?))*[^\\^~`'!@$#=%&*()+|{}:;,><?\"\\/\\[\\]\\\\\\s-\\.]@[^\\^~`'!@$#=%&*()+|{}:;,><?\"\\/\\[\\]\\\\\\s\\.]*[^\\^~`'!@$#=%&*()+|{}:;,><?\"\\/\\[\\]\\\\\\s-\\.]\\.(?!\\.+?)[^\\^~`'!@$#=%&*()+|{}:;,><?\"\\/\\[\\]\\\\0-9\\s-\\_]{2,40}$$
this validation accepting eg: kate@stack---overlow.com
however want restrict domain name after @
, before .
have 1 hyphen.
update:
i not prefer making check using contains
rather make part of regex.
i'd recommend first validating email address javamail api, described in answer: validate e-mail field using regex. way don't have deal complicated regex handle of details of rfc 822 specification on email addresses.
once passes that, add additional check single hyphen after @
, before .
, e.g.:
public boolean isvalidemail(string email) { try { string address = new internetaddress(email).getaddress(); return address.matches(".*@[^-]*-{0,1}[^-]*\\..*"); } catch (addressexception e) { // should log here debugging return false; } }
Comments
Post a Comment