用c#,regex隐藏一些字符的电子邮件地址

tug*_*erk 9 c# regex security email

我想用*char替换一些电子邮件地址的字符.

当客户提出请求时,我想隐藏下面的电子邮件地址的一些字符;

ha~~~~@~~~~ail.com

我想这样做.我希望在@之前显示前两个字符,在@之后显示最后3个字符

但还有其他常见的方法吗?

Bra*_*tie 10

与其他回复类似,也有所不同.也接受.co.uk地址.

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[]{
                        "joe@example.com",
                        "jim@bob.com",
                        "susie.snowflake@heretoday.co.uk",
                        "j@b.us",
                        "bc@nh.us"
                });
                tests.ForEach(email =>
                {
                        Console.WriteLine(Regex.Replace(email, regex, replace));
                });
        }
}
Run Code Online (Sandbox Code Playgroud)

结果是:

jo*@*le.com
ji*@*ob.com
su*@*co.uk
j@b.us
bc@nh.us
Run Code Online (Sandbox Code Playgroud)

虽然我不是百分之百确定你想要用两边只有2个字母的名字做什么(因此最后两个结果).但这是我的出价.