using System;
using System.Text.RegularExpressions;
namespace Recetas.Ch01
{
///<summary>
/// Conjunto de utilidades para la validación de recursos Web: URL, email.
///</summary>
public static class UtilitarioWeb
{
///<summary>
/// Valida una dirección de email: id@dominio
///</summary>
/// <param name="email">Dirección de email a validar.</param>
///<returns>
/// true si la dirección de correo electrónico es válida, false en caso contrario.
///</returns>
public static bool ValidarEmail(string email)
{
Regex regex = new Regex(@"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@" + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\." + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|" + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$");
return regex.IsMatch(email);
}
///<summary>
/// Valida una dirección Web.
///</summary>
/// <param name="url">URL a validar.</param>
/// <returns>
/// true si la dirección es valida, y false si no lo es.
///</returns>
public static bool ValidarUrl(string url)
{
string pattern = @"^http(s)?://([\w-]+.)+[\w-]+(/[\w- ./?%&=])?$";
Regex reg = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
return reg.IsMatch(url);
}
public static void Main(string[] args)
{
Console.WriteLine(UtilitarioWeb.ValidarUrl("http://o...content-available-to-author-only...t.com"));
Console.WriteLine(UtilitarioWeb.ValidarEmail("fernd@outlook.com"));
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uVGV4dC5SZWd1bGFyRXhwcmVzc2lvbnM7CgpuYW1lc3BhY2UgUmVjZXRhcy5DaDAxCnsKCS8vLzxzdW1tYXJ5PgoJLy8vIENvbmp1bnRvIGRlIHV0aWxpZGFkZXMgcGFyYSBsYSB2YWxpZGFjacOzbiBkZSByZWN1cnNvcyBXZWI6IFVSTCwgZW1haWwuCgkvLy88L3N1bW1hcnk+CglwdWJsaWMgc3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