It's not a secret anymore!
The moose likes HTML, CSS and JavaScript and the fly likes First hand at RegEx Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "First hand at RegEx" Watch "First hand at RegEx" New topic

First hand at RegEx

Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Hi guys

I need to do some validation on my system registration screen and decided to use RegEx to validate the email address.

I found a handy little website that allows you to edit and test RegEx to see if you get the correct resulst.
mine should bascially allow for something like

This is what I came up with

It all works fine, apart for some reason it doesn't like it if you put a - in your email!
From what I can see it should allow

but it doesn't!


Any suggestions?

[ June 26, 2008: Message edited by: Keith Seller ]
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42958
Try putting the hyphen first, like [-a-zA-Z0-9\\_\\.]

But: If you try to make the regexp overly specific it can happen all too easily that it disallows some proper addresses. I generally use "^\\S+@([-\\w]+\\.){1,4}[a-z]{2,6}$" with a few additional checks (like a valid TLD).

One problems I see with the one you posted is that various valid TLDs that have more than 3 characters (like .travel, .museum and .name) would fail to be recognized.
[ June 26, 2008: Message edited by: Ulf Dittmer ]
I agree. Here's the link:
subject: First hand at RegEx
jQuery in Action, 3rd edition