Advertisement
Assi

13. Strings and Text Processing 18. Email

Jan 26th, 2013
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. class ExtractEmails
  5. {
  6.     static void Main()
  7.     {
  8.         string str = "A @telerik.com. This baj.ivan@yahoo.co.uk. This is   also: ISTRUEexa_mple@abv.bg ";
  9.         int atIndex = 0;
  10.         int startIndex = 0;
  11.         int firstDotIndex = 0;
  12.         int lastDotIndex = 0;
  13.         int endIndex = 0;
  14.        
  15.         bool at = false;
  16.         bool letterBeforeAt = false;
  17.         bool letterAfterAt = false;
  18.         bool dotAfterAtAfterLetter = false;
  19.         bool letterAfterDot = false;
  20.         bool ident = false;
  21.         bool domain = false;
  22.  
  23.         for (int i = 0; i < str.Length - 5; i++)
  24.         {
  25.             if (str.Substring(i, 1) == "@")
  26.             {
  27.                 at = true;
  28.                 atIndex = i;
  29.                 if (char.IsLetter(str[i - 1]))
  30.                 {
  31.                     startIndex = str.LastIndexOf(" ", atIndex)+1;
  32.                     letterBeforeAt = true;
  33.                 }
  34.                 if (char.IsLetter(str[i + 1]))
  35.                 {
  36.                     letterAfterAt = true;
  37.                 }
  38.  
  39.                 endIndex = str.IndexOf(" ", atIndex);
  40.  
  41.                 for (int k = atIndex + 2; k < endIndex; k++)
  42.                 {
  43.                     if (str.Substring(k, 1) == ".")
  44.                     {
  45.                         dotAfterAtAfterLetter = true;
  46.                         firstDotIndex = k;
  47.                         if (char.IsLetter(str[firstDotIndex + 1]))
  48.                         {
  49.                             letterAfterDot = true;
  50.                         }
  51.                     }
  52.  
  53.                 }
  54.                 lastDotIndex = str.LastIndexOf(".", atIndex);
  55.                 if (char.IsLetter(str[lastDotIndex+1]))
  56.                 {
  57.                     domain = true;
  58.                    
  59.                 }
  60.  
  61.                 if (atIndex - startIndex > 6)
  62.                 {
  63.                     ident = true;
  64.                 }
  65.                 if (endIndex - lastDotIndex > 3)
  66.                 {
  67.                     domain = true;
  68.                 }
  69.                
  70.             }
  71.             if (at && letterBeforeAt && letterAfterAt &&
  72.                 dotAfterAtAfterLetter && letterAfterDot && ident && domain)
  73.             {
  74.                 int length = endIndex - startIndex;
  75.                 Console.WriteLine(str.Substring(startIndex, length));
  76.  
  77.                 at = false;
  78.                 letterBeforeAt = false;
  79.                 letterAfterAt = false;
  80.                 dotAfterAtAfterLetter = false;
  81.                 letterAfterDot = false;
  82.                 ident = false;
  83.                 domain = false;
  84.             }
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement