|
-
Apr 27th, 2005, 10:28 PM
#1
Thread Starter
Dazed Member
Need help with regex(part II)
I wrote a pattern which validates email addresses. Its seems to work but i wanted to see if anyone could come up with some variations and possibly some tips. Thanks.
Code:
String email = new String("(?:\\w+?\\@{1}\\w+?)\\.{1}(?:com|net|org|edu)");
-
Apr 28th, 2005, 06:04 AM
#2
Re: Need help with regex(part II)
 Originally Posted by Dilenger4
I wrote a pattern which validates email addresses. Its seems to work but i wanted to see if anyone could come up with some variations and possibly some tips. Thanks.
Code:
String email = new String("(?:\\w+?\\@{1}\\w+?)\\.{1}(?:com|net|org|edu)");
An email address typically consists of two major parts:
- Mailbox - the can contain letters, numbers, underscores, hyphens and can also contain a + sign as well as dots of course. So I would recommend you use the ( . ) to match this part:
- Fully Qualified domain name / host name - If I were you I wouldn't chck for valid TLD's, what about .org.uk and .sch.uk and .de and ac.uk and .mil?
You can however check that it contains only letters, numbers and hyphens. But they cannot contain underscores, so you can't use \w.
Code:
PCRE:
/^.+@((?i)[a-z0-9\-]+(\.(?i)[a-z0-9\-]+)?)+$/
-
Apr 28th, 2005, 07:57 PM
#3
Thread Starter
Dazed Member
Re: Need help with regex(part II)
Thanks for replying visualAd. Yeah i didn't take into account that an email address might contain hyphens, underscores and dots. The + sign ive never seen used in an address though. Ive just been using the code below to test the patterns i create. I found the following expression which might be better suited. "(\\w[\\-.\\w]*.*@\\w+\\.(?:com|net|org))". I didn't create it so im a bit shady on how it works. I guess it tests for a word character \\w(dont know why they didn't specify a quanitifer), [\\-.\\w](Guess it's supposed to be read "-" or "." or just a word character or set of words), then more words, don't know why the @ isn't escaped then well we get the rest .
Code:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class mailval{
public static void main(String[] args){
String email = new String("(?:\\w+?\\@{1}\\w+?)\\.{1}(?:com|net|org|edu)");
Pattern emp = Pattern.compile(email);
Matcher m = null;
String[] malto = new String[7];
malto[0] = "[email protected]"; // should be true
malto[1] = "whatever@@whatever.com"; // false
malto[2] = "whatever@@whatever.biz"; // false
malto[3] = "[email protected]"; // true
malto[4] = "[email protected]"; // true
malto[5] = "[email protected]"; //false
malto[6] = "[email protected]"; //false
for(int i = 0; i < malto.length; i++){
if(emp.matcher(malto[i]).matches()){
System.out.println("true");
}else{
System.out.println("false");
}
}
}
}
Last edited by Dilenger4; Apr 28th, 2005 at 08:04 PM.
-
May 1st, 2005, 06:01 AM
#4
Re: Need help with regex(part II)
-
May 1st, 2005, 08:40 AM
#5
Thread Starter
Dazed Member
Re: Need help with regex(part II)
I didn't get to try the pattern you posted. /^.+@((?i)[a-z0-9\-]+(\.(?i)[a-z0-9\-]+)?)+$/ It dosen't seem too far off from a regular expression written in Java though.
-
May 1st, 2005, 09:24 AM
#6
Re: Need help with regex(part II)
Here is a pattern that really allows ALL valid e-mail address and NO others.
^[A-Za-z0-9!#-'\*\+\-\/=\?\^_`\{-~]+(\.[A-Za-z0-9!#-'\*\+\-\/=\?\^_`\{-~]+)*@[A-Za-z0-9!#-'\*\+\-\/=\?\^_`\{-~]+(\.[A-Za-z0-9!#-'\*\+\-\/=\?\^_`\{-~]+)*$
It seems complicated, but really is very simple. The core part is this character class:
[A-Za-z0-9!#-'\*\+\-\/=\?\^_`\{-~]
This is the collection of all characters that are valid as normal parts of e-mail addresses. Substitute this by [[:mail:]] and the whole expression becomes:
^[[:mail:]]+(\.[[:mail:]]+)*@[[:mail:]]+(\.[[:mail:]]+)*$
So we have the start of the string, followed by one or more mail characters. Then there are any numbers of groups that consist of a dot followed by one or more mail characters.
Then comes the @.
After that, the same pattern again.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|