Advertisement
APXOHT

Untitled

Jan 25th, 2013
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.24 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. class ExtractEmails
  5. {
  6.     static bool isWord(string input)
  7.     {
  8.         bool isWordCharacter = true;
  9.  
  10.         string availableChars = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM@.-_";
  11.  
  12.         for (int i = 0; i < input.Length; i++)
  13.         {
  14.             int index = availableChars.IndexOf(input[i]);
  15.  
  16.             if (index < 0)
  17.             {
  18.                 isWordCharacter = false;
  19.                 break;
  20.             }
  21.         }
  22.  
  23.         return isWordCharacter;
  24.     }
  25.  
  26.     static void Main()
  27.     {
  28.         //I assume identifier is not less than 6 characters, host is not less than one character and domain is not less than two characters like a normal e-mail
  29.         string input = "Please contact us by phone (+359 222 222 222) or by email at exa_mple@abv.bg or at baj.ivan@yahoo.co.uk. This is not email: test@test. This also: @telerik.com. Neither this: a@a.b.";
  30.        
  31.         string[] separatedInput = input.Split(' ');
  32.  
  33.         for (int i = 0; i < separatedInput.Length; i++)
  34.         {
  35.             string currentWord = separatedInput[i];
  36.  
  37.             if (currentWord[currentWord.Length - 1] == '.')
  38.             separatedInput[i] = currentWord.Remove(currentWord.Length - 1);
  39.         }
  40.  
  41.         bool hasIdentifier = false;
  42.         bool hasHostAndDomain = false;
  43.  
  44.         for (int i = 0; i < separatedInput.Length; i++)
  45.         {
  46.             if (!isWord(separatedInput[i]))
  47.             {
  48.                 continue;
  49.             }
  50.  
  51.             int indexAt = separatedInput[i].IndexOf("@");
  52.  
  53.             if (indexAt > 5)
  54.             {
  55.                 hasIdentifier = true;
  56.             }
  57.            
  58.             int indexDot = separatedInput[i].IndexOf(".", indexAt + 1);
  59.  
  60.             if (indexDot > indexAt + 1)
  61.             {
  62.                 hasHostAndDomain = true;
  63.             }
  64.  
  65.             if (hasIdentifier & hasHostAndDomain)
  66.             {
  67.                 if ((separatedInput[i].Substring(indexDot, separatedInput[i].Length - indexDot - 1)).Length > 1)
  68.                 {
  69.                     Console.WriteLine(separatedInput[i]);
  70.                 }
  71.             }
  72.  
  73.             hasIdentifier = false;
  74.             hasHostAndDomain = false;
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement