# Parsing an email string

My program needs to parse an e-mail string. There are two possibilities to enter an e-mail-address. Either with the alias or without one, plain the e-mail-address.

1st possibity:

string addressWithAlias = "test my address <bla@blub.com>";


2nd possibility:

string addressWithoutAlias = "bla@blub.com";


So, I wrote two functions:

private static string[] getAddressPartsRegex(string address)
{

Regex reg = new Regex(@"(.+?(?=<))<(.*@.*?)>");

return gr.Count == 1
: new[] { gr[1].Value.Trim(), gr[2].Value.Trim() };
}

{
}


They both work fine and the results are the same. One uses regex, the other uses Split and Join. What would you suggest to use, and why? What is the more beautiful function? Are there any bugs I didn't see?

• @BCdotWEB the is the standard when writing an e-mail but you won't see the e-mail address, you see a name. Something like James Bond <james.bond@mi6.co.uk> will display in outlook just James Bond instead of his e-mail-address. So, this is kind of a standard :) – Matthias Burger Mar 1 '18 at 13:06
• I mean... There are way more ways to send email addresses than you listed. here, here, here for examples – Dan Obermiller Mar 1 '18 at 15:16
• Ultimately, the only valid email address is one that you can send email to; it is a lot more useful to see if your sending tool of choice can handle the email address than the arbitrary (hopefully subset, but notalways) criteria from the RFC you chose to enforce. Even then, you can't validate that it is a real email address - the only way to do that is by sending it. – Dan Obermiller Mar 1 '18 at 15:18
• ex-parrot.com/~pdw/Mail-RFC822-Address.html – MCMastery Mar 1 '18 at 15:37
• @MatthiasBurger lol just found that and it made me laugh – MCMastery Mar 1 '18 at 15:46

Consider taking advantage of existing features that could provide an additional layer of validation.

mainly System.Net.Mail.MailAddress

Also as mentioned in a comment, no need to be creating the regular expression every time the function is called.

static Regex mailExpression = new Regex(@"(.+?(?=<))<(.*@.*?)>");

return groups.Count == 1
}


According to reference source code, internally MailAddress will try to parse the address given to it.

This avoids having to roll your own parser as one already exists out of the box that has been tried, tested and is stable.

private static MailAddress getAddress(string address) {

}


You have the added advantage of having a strongly typed object model to work with that will provide you with usable properties.

The following Unit Test demonstrates the desired behavior.

[TestClass]
public class EmailParserTest {
[TestMethod]
//Arrange
var expectedAlias = "test my address";

//Act

//Assert
.Should()
.NotBeNull()

}

[TestMethod]
//Arrange

//Act

//Assert
.Should()
.NotBeNull()
;
}

}
}

• I'm working with MimeKit.MailboxAddress here, poorly, the Mimekit-library doesn't do me that favour, Mailkit throws error. But you are right, of course :) – Matthias Burger Mar 1 '18 at 15:15
• i now let system.net.mail do this thing. it's better indeed. and interesting unit-test. what lib you use? – Matthias Burger Mar 1 '18 at 15:29
• I used the standard VS testing tools for the test runner and Fluent Assertions to assert – Nkosi Mar 1 '18 at 15:34

Let me suggest an alternative regular expression, that correctly handles both cases:

(.*?)<?(\b\S+@\S+\b)>?


This regular expression correctly identifies both patterns you want to support. Somewhat noteworthy here is the use of \S in the email-address to exclude whitespace characters, which are incorrectly allowed in your orignal regex. That led to accepting something like the following as valid Email specification:

bla bla <te st@ exampl e.com>


Another thing that this regex does is accept Specifications of email adresses that do not require the email to be enclosed in <>. This happens by ensuring the address is surrounded by word boundaries (\b).

You should be able to easily use it like so:

static Regex mailExpression = new Regex(@"(.*?)<?(\b\S+@\S+\b)>?");