File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Regex for email verifying Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regex for email verifying" Watch "Regex for email verifying" New topic
Author

Regex for email verifying

Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Hi.
This is my first week in Regex, I need some help please...
I want to create a regex for verifying an email address.
My requirements are :
username may occupies not more than 15 chars (underscores, digits are allowed, dashes and special chars like % $ ! # ... are not allowed , followed by @ char, followed by hostname, followed by dot char,
and at least 2 chars after the dot.
My first attempt was : [a-z0-9]{15}@[a-z0-9]
Well, ofcourse, it doesn't work.
I don't know how to allow dot char after hostname, and how to disallow special chars in username .
I don't why, but I think Regex are really complecated ...
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

My first attempt was : [a-z0-9]{15}@[a-z0-9]


That means 'exactly 15 chars' before the AT.



perhaps? (not testet!)

Important note:
This regexp should work if read from a file.
If you write it to sourcecode, you have to mask the special char '\' with an additional '\'.
System.out your regexp, and compare it with your expectations, if you use backslashes, if you're lazy in thinking like me.


http://home.arcor.de/hirnstrom/bewerbung
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Thanks, but no, it doesn't work ...
Using your pattern, username can contains special chars like : @ < > & ^ ) ( % -
Same thing for hostname (although, hostname can contains -) ...
Any support ?
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
On another regex thread, Java regular expression grouping , Stan James suggested a tool that might be useful to you since it allows you to tweak your expression and see how it responds -- quick feedback. I haven't checked it out yet.


a tool called RegExCoach that can step through the groups and highlight them one at a time. You can interactively tweak your expressions and see how they respond. See it here: http://www.weitz.de/regex-coach/ This kind of experimentation is a fun way to learn.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

No - John Todd, my Pattern will not accept <>^%@ in username or hostname.



Test it.
[ March 27, 2005: Message edited by: Stefan Wagner ]
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Thanks again Stefan, but no !!
Ok, compile the following :

Try to run it, you get true.
try to include <>^%@, you will get true.
I think, we should not inlcude \\. in the pattern (is it true that \\. means any character ?)
how to prevent the other illegal chars (like !#$*()&) ?
Thanks man.
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Use matches() instead of find() if you want it to see if the pattern matches the entire String.

Output:
Pattern: [a-z0-9_\.]{1,15}@[a-z0-9_]+\.[a-z]{2,}
found: sdsd12@yahoo.com
Pattern: [a-z0-9_\.]{1,15}@[a-z0-9_]+\.[a-z]{2,}
didn't match
[ March 27, 2005: Message edited by: Carol Enderlin ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Good catch, Carol!
Originally posted by John Todd:
is it true that \\. means any character ?
"." outside a character class [] means any character. "\." -- "\\." to escape the "\" for the Java compiler -- escapes the "." making it a literal period.

IIRC, escaping is unnecessary inside a character class (it's always a literal period) since it would make the whole character class equivalent to any character, but I think it's harmless. The escape doesn't become a literal slash, allowing it to sneak through in an email address ("bob\karen@foo.com").
[ March 27, 2005: Message edited by: David Harkness ]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Thanks Carol, for pointing out, that hu@sdsd12@yahoo.com <b> contains </b> but <b> isn't </b> a valid email-adress.

And thanks David, for explaining, why the double-backslashes aren't meaningful inside a groupexpression - I never thought about it, but now, I will easily remember.

so we end up:


Another 'no' from you, John?
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Well, this is another way (I want to use find( ) method )
Pattern p = Pattern.compile("^[a-z0-9_\\.]{1,15}@[a-z0-9_]+\\.[a-z]{2,}$");
Matcher m = p.match("hu@sdsd12@yahoo.com");
System.out.println(m.find( ));
Any way, thanks all Stefan, David(and you Carol )
Gracias.
[ March 30, 2005: Message edited by: John Todd ]
 
 
subject: Regex for email verifying