.NET Framework Bookmark and Share   
 index > Regular Expressions > Regular expression for email address and display name.
 

Regular expression for email address and display name.

I need a regular expression to valid the string below:

Richard<abc@test.com>;Leo Mark<gsg@get.com>;O'Neil Amy<oamgk@test.co.uk>;Rachel Baileys<sagle@test.net>


The format is: DisplayName<EmailAddress>;DisplayName<EmailAddress>;DisplayName<EmailAddress>

Can anyone help?

Thanks in advance!
waterding
The following works for your example...
            string pattern = @"^((?<displayname>[\w\s'-]+)(?<address><[\w]+@([\w\d-]+\.)+[\w]{2,4}>)(\;|$))+$";
            string test = @"Richard<abc@test.com>;Leo Mark<gsg@get.com>;O'Neil Amy<oamgk@test.co.uk>;Rachel Baileys<sagle@test.net>";
            Console.WriteLine("{0}", Regex.IsMatch(test, pattern));


Les Potter, Xalnix Corporation, Yet Another C# Blog
xalnix
The following works for your example...
            string
 pattern = @"^((?<displayname>[\w\s'-]+)(?<address><[\w]+@([\w\d-]+\.)+[\w]{2,4}>)(\;|$))+$"
;
            string
 test = @"Richard<abc@test.com>;Leo Mark<gsg@get.com>;O'Neil Amy<oamgk@test.co.uk>;Rachel Baileys<sagle@test.net>"
;
            Console.WriteLine("{0}"
, Regex.IsMatch(test, pattern));


Les Potter, Xalnix Corporation, Yet Another C# Blog

Thanks, xalnix.

How can I extract the DisplayName and Email?
waterding
This is a little convoluted, but I believe it is the accepted way in such a situation...

            string pattern = @"^((?<displayname>[\w\s'-]+)(?<address><[\w]+@([\w\d-]+\.)+[\w]{2,4}>)(\;|$))+$";
            string test = @"Richard<abc@test.com>;Leo Mark<gsg@get.com>;O'Neil Amy<oamgk@test.co.uk>;Rachel Baileys<sagle@test.net>";
            if(Regex.IsMatch(test, pattern))
            {
                Match mx = Regex.Match(test,pattern);
                for(int idx=0; idx < mx.Groups["displayname"].Captures.Count; ++idx)
                {
                    Console.WriteLine("DisplayName: {0}, Email: {1}",
                        mx.Groups["displayname"].Captures[idx].Value,
                        mx.Groups["address"].Captures[idx].Value);
                }
            }


Les Potter, Xalnix Corporation, Yet Another C# Blog
xalnix
This is a little convoluted, but I believe it is the accepted way in such a situation...

            string
 pattern = @"^((?<displayname>[\w\s'-]+)(?<address><[\w]+@([\w\d-]+\.)+[\w]{2,4}>)(\;|$))+$"
;
string test = @"Richard<abc@test.com>;Leo Mark<gsg@get.com>;O'Neil Amy<oamgk@test.co.uk>;Rachel Baileys<sagle@test.net>" ;
if (Regex.IsMatch(test, pattern))
{
Match mx = Regex.Match(test,pattern);
for (int idx=0; idx < mx.Groups["displayname" ].Captures.Count; ++idx)
{
Console.WriteLine("DisplayName: {0}, Email: {1}" ,
mx.Groups["displayname" ].Captures[idx].Value,
mx.Groups["address" ].Captures[idx].Value);
}
}


Les Potter, Xalnix Corporation, Yet Another C# Blog
Thanks xalnix.

I just found out that the Regx doesn't work for this string:

abc<abc.def@test.com>

Can you help?
waterding
Try modifying the pattern to the following. The only difference is I added '.' to the characters allowed to the left of the '@'.

@"^((?<displayname>[\w\s'-]+)(?<address><[\w\.]+@([\w\d-]+\.)+[\w]{2,4}>)(\;|$))+$"


Les Potter, Xalnix Corporation, Yet Another C# Blog
xalnix
string pattern = @"^((?<displayname>[\w\s'-]+)(?<address><[\w\.]+@([\w\d-]+\.)+[\w]{2,4}>)(\;|$))+$";
if (!Regex.IsMatch(txtFromEmail.Text, pattern))
{
cvFromEmailInvalid.IsValid = false;
isValid = false;
}


I am using this regx to relidate. However, it doesn't work for this: Richard<abc@test.com>;Leo Mark<gsg@get.com>;O'Neil Amy<o'neil.amy@test.co.uk>;Bailes O'Neil<Bailes.O'Neil@test.net>
waterding

You can use google to search for other answers

Custom Search

More Threads

• app.config update
• RegEx for Hexidecimal numbers
• Regex - Expression Not Working Correctly in VB2005 Express
• regex for arithmetic expression
• Problem with end of line
• Expression with string and date
• check for substring by RegularExpressionValidator
• Need Help With Simple RegEx
• find function in string with Regex
• Regular expression for All image links