Advertisement
elena1234

ExtractEmails*-regex

Nov 24th, 2020
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.84 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3. namespace text
  4. {
  5.     class MainClass
  6.     {
  7.         public static void Main(string[] args)
  8.         {
  9.             string input = Console.ReadLine();
  10.             Regex regexForEmail=new Regex(@"(?<=\s)(?<user>(?![_])[A-za-z0-9]+(?:[\.\-_][A-za-z0-9]+)*)@(?<host>[a-zA-Z]+(?:[\-][a-zA-z]+)*(?:\.[a-zA-Z]+(?:[\-][a-zA-Z]+)*)*\.[a-z]+)");// NB! VS is not running regex with "\_"
  11.             MatchCollection matchesEmail = regexForEmail.Matches(input);
  12.             if (regexForEmail.IsMatch(input))
  13.             {
  14.                 foreach (Match match in matchesEmail)
  15.                 {
  16.                     Console.Write(match.Groups["user"]);
  17.                     Console.Write("@");
  18.                     Console.WriteLine(match.Groups["host"]);
  19.                 }
  20.             }
  21.  
  22.         }
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement