Advertisement
dimipan80

Extract Emails

May 12th, 2015
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.54 KB | None | 0 0
  1. /* Write a program to extract all email addresses from given text.
  2.  * The text comes at the first input line.
  3.  * Print the emails in the output, each at a separate line.
  4.  * Emails are considered to be in format <user>@<host>, where:
  5.  * <user> is a sequence of letters and digits, where '.', '-' and '_' can appear between them.
  6.  * Examples of valid users: "stephan", "mike03", "s.johnson", "st_steward", "softuni-bulgaria",
  7.  * "12345". Examples of invalid users: ''--123", ".....", "nakov_-", "_steve", ".info".
  8.  * <host> is a sequence of at least two words, separated by dots '.'.
  9.  * Each word is sequence of letters and can have hyphens '-' between the letters.
  10.  * Examples of hosts: "softuni.bg", "software-university.com", "intoprogramming.info",
  11.  * "mail.softuni.org". Examples of invalid hosts: "helloworld", ".unknown.soft.",
  12.  * "invalid-host-", "invalid-".
  13.  * Example of valid emails: info@softuni-bulgaria.org, kiki@hotmail.co.uk,
  14.  * no-reply@github.com, s.peterson@mail.uu.net, info-bg@software-university.software.academy. */
  15.  
  16. namespace _03.ExtractEmails
  17. {
  18.     using System;
  19.     using System.Text.RegularExpressions;
  20.  
  21.     class ExtractEmails
  22.     {
  23.         static void Main(string[] args)
  24.         {
  25.             string text = Console.ReadLine();
  26.             string pattern = @"[A-Za-z]+[.-_]*[A-Za-z]+@[A-Za-z]+[-]*[.A-Za-z]+[A-Za-z]";
  27.  
  28.             MatchCollection matches = Regex.Matches(text, pattern);
  29.             foreach (Match match in matches)
  30.             {
  31.                 Console.WriteLine(match);
  32.             }
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement