using System.Text.RegularExpressions;
public static void Main()
Console.WriteLine("silvano.amaral@grupoboticario.com.br :::: "+ formatEmail("silvano.amaral@grupoboticario.com.br"));
Console.WriteLine("silvano@gmail.com :::: "+ formatEmail("silvano@gmail.com"));
Console.WriteLine("silva@gmail.com :::: "+ formatEmail("silva@gmail.com"));
Console.WriteLine("silv@gmail.com :::: " + formatEmail("silv@gmail.com"));
Console.WriteLine("sil@gmail.com :::: " + formatEmail("sil@gmail.com"));
Console.WriteLine("si@gmail.com :::: " + formatEmail("si@gmail.com"));
Console.WriteLine("teste@globo.com :::: " + formatEmail("teste@globo.com"));
Console.WriteLine("bruno.leite@gmail.com :::: " + formatEmail("bruno.leite@gmail.com"));
Console.WriteLine("119947363023 :::: " + formatPhone("119947363023"));
Console.WriteLine("36001129 :::: " + formatPhone("36001129"));
Console.WriteLine("01234567890 :::: " + formatCPF("01234567890"));
public static string formatEmail(string email) {
var pattern = @"(?<=.{5})[^@\n](?=[^@\n]*?@)|(?:(?<=@.{5})|(?!^)\G(?=[^@\n]*$)).(?=.*[^@\n]\.)";
return Regex.Replace(email, pattern, m => new string('*', m.Length));
public static string formatPhone(string fone) {
var pattern = @"(?<=.{0})(\d+)";
var primeiraParte = fone.Substring(0, fone.Length - 4);
var segundaParte = fone.Substring(fone.Length - 4);
return String.Concat(Regex.Replace(primeiraParte, pattern, m => new string('*', m.Length)), "-", segundaParte);
public static string formatCPF(string cpf) {
var pattern = @"(\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2})|(\d{3}\.\d{3}\.\d{3}\-\d{2})";
var exemplo = Regex.Replace(cpf, @"(?<=.{3})[^@\n]", m => new string('*', m.Length));