我想用 * 字符替换电子邮件地址的一些字符。
当客户提出要求时,我想隐藏一些电子邮件地址的字符,如下所示;
哈哈~~~~@~~~~ail.com
我想那样做。我想在 @ 之前显示前两个字符,在 @ 之后显示最后 3 个字符
但是还有其他通用的方法吗?
最佳答案
与其他响应相似,但也不同。也接受 .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));
});
}
}
结果:
jo*@*le.com
ji*@*ob.com
su*@*co.uk
j@b.us
bc@nh.us
虽然我不是 100% 确定你想对两边只有 2 个字母的名字做什么(因此是最后两个结果)。但这是我的出价。 <强> Example
关于c# - 电子邮件地址用 C#、正则表达式隐藏一些字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5107309/