How To Parse String in format “Name

2019-02-14 11:12发布

I have a method which receives a contact in one of the following formats:

1 - "email@domain.com"

2 - "Name <email@domain.com>" OR "Name<email@domain.com>" (Spaces can exist)

If it is in format (1) I do nothing. In case of (2) I need to parse the name and email.

I never know in which format I will get the emails. But it will be one of the two.

How can I do this?

6条回答
Explosion°爆炸
2楼-- · 2019-02-14 11:28

MailAddress is a great solution, but unfortunately System.Net.Mail has not yet been ported to .NET Core.

I find that the following Regex solution works for reasonably well-formed inputs:

var re = new Regex(@"""?((?<name>.*?)""?\s*<)?(?<email>[^>]*)");
var match = re.match(input);

var name = match.Groups["name"].Value;
var email = match.Groups["email"].Value;

I have tested this with the following kinds of inputs:

bob@example.com
<bob@example.com>
Bob Example <bob@example.com>
Bob Example<bob@example.com>
"Bob Example" <bob@example.com>
"Example, Bob J." <bob@example.com>
查看更多
走好不送
3楼-- · 2019-02-14 11:32

There is actually already a .NET class called MailAddress that can do this for you quite simply.
UPDATE: It can not only get the display name but also the email address, username, and host.

First include using System.Net.Mail and then you can get the info with something like this:

MailAddress email = new MailAddress("Johnny <johnny@example.com>");
string displayName = email.DisplayName;
string address = email.Address;
string user = email.User;
string host = email.Host;

This will work with the two scenarios that you described so "Name <email@domain.com>" and "Name<email@domain.com>" both work and give you Name. I went on and created a test that can be found here that will give you the sample output of:

'email@domain.com' =
   DisplayName = ''
   Address = 'email@domain.com'
   User = 'email'
   Host = 'domain.com'
'Name<email@domain.com>' =
   DisplayName = 'Name'
   Address = 'email@domain.com'
   User = 'email'
   Host = 'domain.com'
'Name <email@domain.com>' =
   DisplayName = 'Name'
   Address = 'email@domain.com'
   User = 'email'
   Host = 'domain.com'
查看更多
看我几分像从前
4楼-- · 2019-02-14 11:35

There's few ways to do that, but first one that occurred was

String[] parts = someValue.Trim().Split(new char[] {' '},false);

if (parts.Length > 1) then
{
// in format "Name OR Name" or more correctly not in format someone@domain.com
}
查看更多
Animai°情兽
5楼-- · 2019-02-14 11:36

You may use Regex:

        var str = "Name<email@domain.com>";

        var r = new Regex("(?<name>.*?)<(?<email>.*?)>");

        if(r.IsMatch(str))
        {
            var m = r.Match(str);
            Console.WriteLine(m.Groups["name"].Trim());
            Console.WriteLine(m.Groups["email"]);
        }

Here I assume that you needn't validate email format and name doesn't have < or > characters

查看更多
虎瘦雄心在
6楼-- · 2019-02-14 11:42

You could use a regex.

^((.*?)\s*?<)(.*?)>?$

The name (if available) would be in the second capturing group, and the email would be in the third.

Example:

var match = Regex.Match("Name <email@domain.com>", @"^((.*?)\s*?<)(.*?)>?$");
string name = math.Groups[2].Value;
string email = match.Groups[3].Value;
查看更多
Melony?
7楼-- · 2019-02-14 11:43

You can either design a simple regular expression (which would, in my opinion, be the elegant solution in this case) or call Split() with '<' as the separator, Trim() the first string and remove the closing '>' from the second one.

查看更多
登录 后发表回答