正则表达式匹配

在C#中使用正则表达会大大提高开发效率.如当你要匹配一个IPV4的地址的时候

 


string ip4 = "192.168.1.5";//你要匹配的地址
 string pattern4 = @"^((([01]?\d\d?|2[0-4]\d|25[0-5])\.){3}([01]?\d\d?|2[0-4]\d|25[0-5]))$";//匹配公式

bool  isIP=Regex.IsMatch(ip4,pattern4);//判断是否匹配成功;

Console.WriteLine(isIP);

string str1 = @"^\d*$";//正则表达式表示一个数字字符串;
 string str2="1s";//Console.ReadLine();
 bool flag=Regex.IsMatch(str2, str1);//只要有一个符合则认为是满足的

string str3 = "how to solve the problem has become to hot issue among many people";
 string pattern2=@"[^how]";
 Console.WriteLine(Regex.Replace(str3,pattern2,"*"));//把str3中出现的h,o,w字符替换为*;