svetlozar_kirkov

Extract e-mails (Exercise)

Oct 11th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.13 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. namespace ConsoleTests
  5. {
  6.     class ConsoleTests
  7.     {
  8.         static void Main()
  9.         {
  10.             string input = Console.ReadLine();
  11.             ExtractMails(input);
  12.         }
  13.         public static void ExtractMails(string text)
  14.         {
  15.             const string MatchEmailPattern =
  16.            @"(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
  17.            + @"((([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])\."
  18.              + @"([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}|"
  19.            + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})";
  20.             Regex rx = new Regex(MatchEmailPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
  21.             // Find matches.
  22.             MatchCollection matches = rx.Matches(text);
  23.             // Report the number of matches found.
  24.             int noOfMatches = matches.Count;
  25.             // Report on each match.
  26.             foreach (Match match in matches)
  27.             {
  28.                 Console.WriteLine(match.Value.ToString());
  29.             }
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment