• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regular Expression for checking a valid email address

 
Ranch Hand
Posts: 33
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because Not-space (\\S+) includes @.
 
Ranch Hand
Posts: 704
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic