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 ...
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.
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 ?
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.
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.
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 ]
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.
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 ]
Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
a bit of art, as a gift, the permaculture playing cards