Regex.IsMatch

在输入字符串中是否有符合正则表达式的项

例子1是否符合手机号码

            Console.WriteLine(Regex.IsMatch(src, @"^[1][35]\d{9}$"));

^$分别表示字符串首尾位置。匹配第一个是1,第二个为3或者5,剩余9个数字总共11数字的字符串。

 

 例子2.是否符合IP地址

  • 2(5[0-5]|[0-4]\d) 匹配:200 ~ 255
  • [0-1]?\d{1,2} 匹配:0 ~ 199
  • 判断字符串是否是IP地址
    ^((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}$

 Regex.Replace 

例子1:将NAME=XXX;替换为NAME=Jack;

string line = "ADDR=1234;NAME=ZHANG;PHONE=6789";
Regex reg = new Regex("NAME=(.+);");
string modified = reg.Replace(line, "NAME=Jack;");

 例子2:将123abc456ABC789中的ABC 不区分大小写替换成123|456|789

var source = "123abc456ABC789";
// 静态方法
//var newSource=Regex.Replace(source,"abc","|",RegexOptions.IgnoreCase);
// 实例方法
Regex regex = new Regex("abc", RegexOptions.IgnoreCase);
var newSource = regex.Replace(source, "|");
Console.WriteLine("原字符串:"+source);
Console.WriteLine("替换后的字符串:" + newSource);
Console.ReadLine();
View Code

相关文章:

  • 2021-12-14
  • 2021-10-03
  • 2022-01-25
  • 2021-12-14
猜你喜欢
  • 2021-06-02
  • 2022-12-23
  • 2021-11-11
  • 2022-12-23
  • 2021-08-09
  • 2021-12-04
相关资源
相似解决方案