给定字符串 test : Shanghai province & beijing
第一:匹配test中是否包含给定的关键字: SHANGHAI|YUNNAN|GUANGZHOU|BEIJING
第二:替换掉包含的关键字为 空字符串
效果如下:
C# 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace KYC.ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string test = "Shanghai province & beijing City";
MatchCollection restult = Regex.Matches(test.ToUpper(), "SHANGHAI|YUNNAN|GUANGZHOU|BEIJING");
foreach (Match item in restult)
{
Console.WriteLine("包含的关键字:" + item.Value);
}
string result = Regex.Replace(test.ToUpper(), "SHANGHAI|YUNNAN|GUANGZHOU|BEIJING", "");
Console.WriteLine("替换后的结果:" + result);
Console.Read();
}
}
}