Validating Email address with System.Text.RegularExpressions.RegEx

  • Thread starter Thread starter Doug
  • Start date Start date
D

Doug

I'm a little confused by this functionality. It doesn't seem to be
behaving like it should.

I am using the following regular expression to validate email
addresses:
"\w+([-+.]\w+)*@\w+([-.]\w+)*\.([a-zA-Z]{2,4})\040*". From what I can
determine it should validate the following rules:

1. BEFORE THE AMPERSAND
A. Must contain at least one alphanumeric character.
B. Can contain a '-', '+', or '.' character but if it does it
must have a alphanumeric character on either side of it.
2. AFTER THE AMPERSAND BUT BEFORE THE '.'
A. Must contain at least one alphanumeric character.
B. Can contain a '-', or '.' character but if it does it must
have a alphanumeric character on either side of it.
3. AFTER THE '.'
A. Must contain at least two alphabetical characters but no more
than 4.

However when I use System.Text.RegularExpressions.RegEx with this
expression and use the IsMatch method and use an email address like
(e-mail address removed)@[email protected] it returns true as if that was
a valid email address based on the rules.

Doesn't the {2,4} mean that it has to have a minimum of 2 characters
after the '.' character but no more than 4? Also, doesn't the
[a-zA-Z] mean that the characters after the '.' must be alphabetical
only? My tests seem to prove otherwise.

Any help on this would be great...thanks!
 
I've looked at that site before I sent this and am still confused.
The problem I am having is with this part:

..([a-zA-Z]{2,4})\040*

That, according to www.regexlib.com seems to indicate that I cannot
have more than 4 characters after the '.' but have to have at least
two. It also seems to indicate that the characters after the '.' have
to be alphabetical. However, neither of these is actually the case
when I run any kind of test.
 
I've looked at that site before I sent this and am still confused.
The problem I am having is with this part:

.([a-zA-Z]{2,4})\040*

That, according to www.regexlib.com seems to indicate that I cannot
have more than 4 characters after the '.' but have to have at least
two. It also seems to indicate that the characters after the '.' have
to be alphabetical. However, neither of these is actually the case
when I run any kind of test.

Regular Expression will return a match if it is able to match anything
in your string.

E.g.
b+\.c (seems to match bc, bbc, bbbc, bbbbc)

But it actually also matches:
abc, zbc, cbc

the regular expression pattern matches the last part of this.
(e-mail address removed)@[email protected]
ie, this part:
(e-mail address removed)

You probably want to look at the ^ and $ atomic symbols.
"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.([a-zA-Z]{2,4})$"

---

I don't understand your second post Doug. Your intepretation is
correct, but what do you mean neither is the case? What did you test
it with and what was the outcome?


jliu - www.ssw.com.au - johnliu.net
 
Back
Top