email address hiding some characters with c#, regex
Because your rules are quite simple it might be easier to just use substring to get the characters before and after the @ and then replace them.
Something along the lines of
int index = email.IndexOf('@');
string returnValue = email.Replace(email.Substring(index - 3, 3), "***").Replace(email.Substring(index+1,3), "***");
Although you'll need to first validate that the email address contains enough characters before the @ and change accordingly.
Similar to other responses, but also different. Accepts the .co.uk addresses too.
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class Test
{
public static void Main()
{
String regex = @"(.{2}).+@.+(.{2}(?:\..{2,3}){1,2})";
String replace = "$1*@*$2";
List<String> tests = new List<String>(new String[]{
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]"
});
tests.ForEach(email =>
{
Console.WriteLine(Regex.Replace(email, regex, replace));
});
}
}
Results in:
jo*@*le.com
ji*@*ob.com
su*@*co.uk
[email protected]
[email protected]
Though I'm not 100% sure what you want to do with names that only have 2 letters on either side (thus the last two results). But that's my bid. Example