-- Listing 16.2

using System.Data.SqlTypes;
using System.Text.RegularExpressions;
namespace Helion.Examples
{
   public static class UDFExample
   {
      private static readonly Regex email_pattern = new Regex
      (
      // Cz przed znakiem @ ("nazwa lokalna").
      "^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*" +
      // Nazwa domeny po znaku @.
      "@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+" +
      // Domena najwyszego poziomu.
      "(?:[a-z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)\\b$"
      );
      [Microsoft.SqlServer.Server.SqlFunction
      (
      IsDeterministic = true
      )]
      public static SqlBoolean EmailMatch(SqlString input)
      {
         SqlBoolean result = new SqlBoolean();
         if (input.IsNull)
            result = SqlBoolean.Null;
         else
            result = (email_pattern.IsMatch(input.Value.ToLower()) == true)
            ? SqlBoolean.True : SqlBoolean.False;
         return result;
      }
   }
}
