Advertisement
Guest User

TA, C# - Part2, Strings and Text Processing, #18

a guest
Jan 21st, 2014
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Text.RegularExpressions;
  7.  
  8. /* #18. Write a program for extracting all email addresses from given text. All substrings that match the format <identifier>@<host>…<domain> should be recognized as emails. */
  9.  
  10. class ExtractEMailAddresses
  11. {
  12.     static void Main()
  13.     {
  14.         Console.WriteLine("Please enter a text below:");
  15.         string inputText = Console.ReadLine();
  16.        
  17.         string[] arrayOfExpressions = inputText.Split(' ');
  18.         Console.WriteLine("The e-mail addresses are:");
  19.         for (int i = 0; i < arrayOfExpressions.Length; i++)
  20.         {
  21.             if (Regex.IsMatch(arrayOfExpressions[i], @"[\w., \-]{2,20}@[\w., \-]{2,20}[.]{1}[\w.]{2,6}"))
  22.             {
  23.                 // Remove '.', '?', '!', ';' or "-" signs at the end
  24.                 if (arrayOfExpressions[i][arrayOfExpressions[i].Length - 1] == '.' || arrayOfExpressions[i][arrayOfExpressions[i].Length - 1] == '?' || arrayOfExpressions[i][arrayOfExpressions[i].Length - 1] == '!' || arrayOfExpressions[i][arrayOfExpressions[i].Length - 1] == ';' || arrayOfExpressions[i][arrayOfExpressions[i].Length - 1] == '-')
  25.                 { arrayOfExpressions[i] = arrayOfExpressions[i].Remove(arrayOfExpressions[i].Length-1, 1); }
  26.                 Console.WriteLine("{0}", arrayOfExpressions[i]);
  27.             }
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement