INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Regular expression not working

Regular expression not working

(OP)
I have a regular expression that works fine for emails but it also allows angle brackets "<" and ">".

It crashes the page if there are angle brackets in the email.

My expression is: @"[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}"

I would assume this would not work since I say only allow certain letters and none are angle brackets.

It allows "tom <tom@aol.com>" to go through fine.

Why does it work and what can I do to fix it?

Thanks,

Tom

RE: Regular expression not working

The cuplrit is the period in your second set: [A-Z0-9.-]+
. is a special character in Regex meaning "any character". You are looking for a literal period, hence escape it:
[A-Z0-9\.-]+

Does that do the trick?

Cheers,
MakeItSo

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: Regular expression not working

(OP)
Didn't work.

I have it as:

private const string Regex_Email = @"[A-Z0-9\._%+-]+@[A-Z0-9\.-]+\.[A-Z]{2,4}";

and the code is:

reg = new Regex(Regex_Email, RegexOptions.IgnoreCase);
result = (reg.IsMatch(email));

and the result comes back as true.

RE: Regular expression not working

I think I know the problem:
<tom@aol.com> goes through because tom@aol.com is a match.

Maybe you should rethink your approach and first check for < > addresses, maybe replacing their "@" with [at]" before testing for mail addresses?

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close