111,126
社区成员
发帖
与我相关
我的任务
分享using System;
using System.Text.RegularExpressions;
class A
{
static void Main()
{
string str = @"中国上海 松江容乐路168号 大哈公寓 12 室
中国上海 松江容乐路168号 大哈公寓 12345
中国上海 松江容乐路168号 ";
str = Regex.Replace(str, @"(?m)\d(?=\d*(?:\s*室|\s*$))", "*");
Console.WriteLine(str);
}
}
/* 程序输出:
中国上海 松江容乐路168号 大哈公寓 ** 室
中国上海 松江容乐路168号 大哈公寓 *****
中国上海 松江容乐路168号
*/string result = Regex.Replace(yourStr, @"\d(?=\d*室)", "*");
richTextBox2.Text = result;using System;
using System.Text.RegularExpressions;
class A
{
static void Main()
{
string str = @"中国上海 松江容乐路168号 大哈公寓 128室
中国上海 松江容乐路168号 大哈公寓 128
中国上海 松江容乐路168号 ";
str = Regex.Replace(str, @"(?m)\d+\s*(?=室|$)", "***");
Console.WriteLine(str);
}
}
/* 程序输出:
中国上海 松江容乐路168号 大哈公寓 ***室
中国上海 松江容乐路168号 大哈公寓 ***
中国上海 松江容乐路168号
*/
string str = @"中国上海 松江容乐路168号 大哈公寓 128室
中国上海 松江容乐路168号 大哈公寓 128
中国上海 松江容乐路168号 ";
str = Regex.Replace(str, @"(\d+)(室)", new MatchEvaluator(delegate(Match match) { return new string('*', match.Groups[1].Value.Length) + match.Groups[2].Value; }));
Console.WriteLine(str);