Advertisement
zornitza_gencheva

18.Task: Maching_emails

Sep 3rd, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _18.Task
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. //Task 18: Write a program for extracting all email addresses from given text.
  15. //All substrings that match the format <identifier>@<host>…<domain> should be recognized as emails.
  16.  
  17. string inputText = "Please contact us by phone (+359 222 222 222) or by email at example@abv.bg or at baj.ivan@yahoo.co.uk. This is not email: test@test. This also: @telerik.com. Neither this: a@a.b.";
  18. string pattern = @"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b";
  19. List<string> resultList = new List<string>();
  20. Match regex = Regex.Match(inputText, pattern, RegexOptions.IgnoreCase);
  21.  
  22. while (true)
  23. {
  24. if (regex.Success)
  25. {
  26. string result = regex.Value;
  27. resultList.Add(result);
  28. }
  29. else
  30. {
  31. break;
  32. }
  33. regex = regex.NextMatch();
  34. }
  35.  
  36. foreach (var item in resultList)
  37. {
  38. Console.WriteLine(item);
  39. }
  40. }
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement