Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 06. *Extract Emails
- Write a program to extract all email addresses from a given text. The text comes at the only input line. Print the emails on the console, each at a separate line. Emails are considered to be in format <user>@<host>, where:
- • <user> is a sequence of letters and digits, where '.', '-' and '_' can appear between them.
- o Examples of valid users: "stephan", "mike03", "s.johnson", "st_steward", "softuni-bulgaria", "12345".
- o Examples of invalid users: ''--123", ".....", "nakov_-", "_steve", ".info".
- • <host> is a sequence of at least two words, separated by dots '.'. Each word is sequence of letters and can have hyphens '-' between the letters.
- o Examples of hosts: "softuni.bg", "software-university.com", "intoprogramming.info", "mail.softuni.org".
- o Examples of invalid hosts: "helloworld", ".unknown.soft.", "invalid-host-", "invalid-".
- • Examples of valid emails: [email protected], [email protected], [email protected], [email protected], [email protected].
- • Examples of invalid emails: [email protected], …@mail.bg, [email protected], [email protected], mike@helloworld, [email protected]., s.johnson@invalid-.
- Examples
- Input Output
- Please contact us at: [email protected]. [email protected]
- Just send email to [email protected] and [email protected]
- [email protected] for more information. [email protected]
- Many users @ SoftUni confuse email addresses. [email protected]
- We @ Softuni.BG provide high-quality
- training @ homeor @ class. –- [email protected].
- using System;
- using System.Text.RegularExpressions;
- public class Program
- {
- public static void Main()
- {//(?<=\s|^) - означава да търси съвпадения след всички видове празен интервал(\s) или(|) да съвпада с началната позиция в низа(^).
- //'^' - този символ е отрицание само в [^...] скоби, а в останалите случаи е съвпадение с началната позиция в низ!
- //За съвпадение в крайната позиция на низ се използва '$'!
- //Може да се търсят съвпадения и след края на имейла с (?=\.\s|\s), но в случая няма смисъл. (?<=....)текст(?=....) - това е
- //шаблон за търсене на текст разположен между два критерия преди началото и след края на търсения текст!
- var pattern = @"(?<=\s|^)[A-Za-z\d]+((\.|_|-)?[A-Za-z\d]+)*@[A-Za-z]+((-|\.)?[A-za-z]+)*\.[A-Za-z]+";
- var text = Console.ReadLine();
- var matchEmails = Regex.Matches(text, pattern);
- foreach (Match item in matchEmails)
- {
- Console.WriteLine(item.Value);
- }
- }
- }
Add Comment
Please, Sign In to add comment