• 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

Regex for email verifying

 
clojure forum advocate
Posts: 3479
Mac Objective C Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ...
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Hussein Baghdadi
clojure forum advocate
Posts: 3479
Mac Objective C Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3479
Mac Objective C Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3479
Mac Objective C Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic