• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular Expression for checking a valid email address  RSS feed

 
Nilesh Sanyal
Ranch Hand
Posts: 33
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to check whether an email address is correct or not. I have written following expression:


But, when I give two @ symbols, it is not showing that an invalid email address has been entered, please help.
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because Not-space (\\S+) includes @.
 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This breaks down as follows:
^ //start of the line
[_A-Za-z0-9-\\+]+ // must start with string in the bracket [ ], must contain one or more character(+)
( // start of group #1
\\.[_A-Za-z0-9-]+ // follow by a dot "." and string in the bracket [ ], must contain one or more character(+)
)* // end of group #1, this group is optional (*)
@ // must contain an "@" symbol
[A-Za-z0-9-]+ // followed by a string in the bracket [ ], must contain one or more character (+)
( // start of group #2 - first level TLD checking
\\.[A-Za-z0-9]+ // follow by a dot "." and string in the bracket [ ], must contain one or more character(+)
)* // end of group #2, this group is optional (*)
( // start of group #3 - second level TLD checking
\\.[A-Za-z]{2,} // followed by a dot "." and string in the bracket [ ], with minimum length of 2
) // end of group #3
$ // end of the line
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The official RFC5322 regex is as follows

This came from http://www.regular-expressions.info/email.html.
This page has a number of much smaller "good enough" regular expressions that may suit you.
Example

But they also have some that are somewhat better.
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nigel Browne wrote:

In the context of a regex character set, (+) does not need to be escaped. Also a hyphen (-) should always appear as the first or last character so as not to be confused as a range delimiter. So...
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nilesh Sanyal wrote:I want to check whether an email address is correct or not. I have written following expression:


This already rejects one of the e-mail addresses I use regularly. Hopefully you aren't planning to validate any e-mail addresses in real life; if you are, I strongly suggest you don't.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!