views:

107

answers:

2

I'm using an asp.net Web Forms RegularExpressionValidator Control to validate a text field to ensure it contains a series of email addresses separated by semicolons.

What is the proper regex for this task?

+1  A: 

I think this one will work:

^([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}(;|$))+

Breakdown:

  • [A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4} : valid email (from http://www.regular-expressions.info/)
  • (;|$) : either semicolon or end of string
  • (...)+ : repeat all one or more times

Make sure you are using case-insensitive matching. Also, this pattern does not allow whitespace between emails or at the start or end of the string.

Joel Potter
`(;$|$)?` can be omitted since the string must already end with `(;|$)`.
Bart Kiers
Why the assumption on the top level domain name up to 4 chr?
Maxwell Troy Milton King
this is not working :( I know this question has a "gimmee da codez" vibe but I'm stuck here
Chris McCall
@Bart: You are correct. I did not allow for that. @Maxwell: Most TLDs do not exceed 4. Read the full analysis at http://www.regular-expressions.info/email.html. @Chris: I tested this on the http://regexlib.com/retester.aspx page, which uses the .NET regex engine. There maybe something special about the validators though.
Joel Potter
@Joel: you'll need to add `a-z` to your pattern since the validator control doesn't let you specify the `RegexOptions.IgnoreCase`. @Chris: edit your question with a sample of your input so everyone's on the same page. As @Joel said, whitespace isn't supported in this pattern. Also, try it with the `a-z` added next to all occurrences of `A-Z` to make it case-insensitive: `^([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}(;|$))+`
Ahmad Mageed
@Ahmad I figured that out last night, the final working regex is `^([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}(;|$))+(;$|$)?`
Chris McCall
Thanks @Ahmad. I thought there was a way to specify the flags on the validator control (I haven't used it much).
Joel Potter
+1  A: 

The 'proper' (aka RFC2822) regex is too complicated. Try something like (\S+@[a-zA-Z0-9-.]+(\s*;\s*|\s*\Z))+ Not perfect but should be there 90% (haven't tried it, so it might need some alteration)

Note: Not too sure about \Z it might be a Perl only thing. Try $ as well if it doesn't work.

Maxwell Troy Milton King
Tested it out, works fine. BTW `\Z` is supported in .NET.
Ahmad Mageed