Advertisement
dimipan80

Extract Emails

May 12th, 2015
477
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: [email protected], [email protected],
  14.  
  15. namespace _03.ExtractEmails
  16. {
  17.     using System;
  18.     using System.Text.RegularExpressions;
  19.  
  20.     class ExtractEmails
  21.     {
  22.         static void Main(string[] args)
  23.         {
  24.             string text = Console.ReadLine();
  25.             string pattern = @"[A-Za-z]+[.-_]*[A-Za-z]+@[A-Za-z]+[-]*[.A-Za-z]+[A-Za-z]";
  26.  
  27.             MatchCollection matches = Regex.Matches(text, pattern);
  28.             foreach (Match match in matches)
  29.             {
  30.                 Console.WriteLine(match);
  31.             }
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement