日期:2014-05-17 浏览次数:21009 次
void Main()
{
string str="任意长度的非空字符串";
str=Regex.Replace(str,"(.*)(.{4})$",m=>new string('*',m.Length)+m.Groups[2].Value);
Console.WriteLine(str); //**********空字符串
}
List<string> _list = new List<string>() {
"1",
"22",
"333",
"4444",
"55555",
"666666"
};
_list = _list.Select(a => Regex.Replace(a, @"(?(^[\s\S]{1,4}$)
------解决方案--------------------
^[\s\S]*?(?=[\s\S]{4}$))", b => Regex.Replace(b.Value, @"[\s\S]", "*"))).ToList();
/*
* [0] "1" string
[1] "22" string
[2] "333" string
[3] "4444" string
[4] "*5555" string
[5] "**6666" string
*/