Win a copy of Head First Android this week in the Android forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

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: 8738
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • 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
  • 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: 8738
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • 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: 8738
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • 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: 26910
82
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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic