static void Main(string[] args)
        {

            string _reguler = @"^(0755|0755-)\d{7,8}$";//正则表达式判断电话格式输入是否确
            string[] TextPhone = { "0755-8978456", "07551235897", "0755-1235897", "075512-35897", "07551235-897", "07551212safwe13" };
            foreach (string item in TextPhone)
            {
                Match re = Regex.Match(item.Trim(), _reguler);

                if (re.Success) Console.WriteLine(item + "是电话号码!");
                else
                    Console.WriteLine(item + "不是电话号码");
            }
            //查找www.baidu.com,替换成http://www.baidu.com
            string mystr = "alsflwww.baidu.com\\sl";//要替换的字符串
            string regulerText =@"w{3}\.\w+\.com";//查找内容
            string ReplaceText =@"http://$&";//替换内容
            Console.WriteLine("替换后:"+Regex.Replace(mystr, regulerText, ReplaceText));


//另一个方法:
Regex myRegx = new Regex(regulerText);
string replaceResult= myRegx.Replace(mystr, ReplaceText);
Console.WriteLine("替换后的结果:"+replaceResult);

            Console.ReadKey();
        }

  

posted on 2022-05-27 16:34  sbwynnss  阅读(316)  评论(0)    收藏  举报