extract all email address from a text using c#

后端 未结 5 563
终归单人心
终归单人心 2020-12-12 21:52

Is there a way to extract all email addresses from a plain text using C# .

For example

my email address is mrrame@gmail.com and his email is

5条回答
  •  感动是毒
    2020-12-12 22:30

    Following works

    public static void emas(string text)
            {
                const string MatchEmailPattern =
               @"(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
               + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
                 + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
               + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})";
                Regex rx = new Regex(MatchEmailPattern,  RegexOptions.Compiled | RegexOptions.IgnoreCase);
                // Find matches.
                MatchCollection matches = rx.Matches(text);
                // Report the number of matches found.
                int noOfMatches = matches.Count;
                // Report on each match.
                foreach (Match match in matches)
                {
                    Console.WriteLine(match.Value.ToString());
                }
            }
    

提交回复
热议问题