正则表达式的简单使用

来源:互联网 发布:时间的玫瑰 北岛 知乎 编辑:程序博客网 时间:2024/06/05 17:45

@符号
我们经常在正则表达式字符串前面加上@字符,这样不让编译器去解析其中的转义字符,而作为正则表达式的语法(元字符)存在。

在正则表达式中,\是转义字符. * 是元字符 如果要表示一个\ . *字符的话,需要使用\ . *

using System;using System.Text.RegularExpressions;namespace _003_正则表达式{    class Program    {        static void Main(string[] args)        {            //==================定位元字符========================            string s = "blog.csdn.com";            //^ 匹配开始,,$匹配结束            string res1 = Regex.Replace(s,"^","www.");            string res2 = Regex.Replace(res1, "$", "/Czhenya");            Console.WriteLine(res2);           //输出:www.blog.com/Czhenya            //==================基本语法元字符=====================            //示例一:校验只允许输入数字            string str1 = Console.ReadLine();            //----------通常使用的方法---------            int count = 0;            for (int i = 0; i < str1.Length; i++)            {                if (str1[i] >='0' && str1[i]<='9')                {                    count++;                }                else                {                    break;                }            }            if (count == str1.Length)            {                Console.WriteLine("您输出的密码符合要求,,,");            }            else            {                Console.WriteLine("您输出的密码不符合要求");            }            //--------使用正则表达式-------------            string str2 = @"^\d*$";  //正则表达式  表示数组字符串            bool isTrue = Regex.IsMatch(s, str2);            Console.WriteLine("输入密码是否符合要求:"+isTrue);            //示例二:校验只允许输入除大小写字母、0-9的数字、下划线_以外的任何字            string str3 = @"^\W*$";   //表示上述的正则表达式  测试原理同上            //=====================反义字符====================            //示例:查找除Czhenya这之外的所有字符            string str4 = "blog.csdn.com/Czhenya";            string str5 = @"[^Czhenya]";    //代表除了Czhenya之外的所有字符            string str6 = Regex.Replace(str4,str5,"*");            Console.WriteLine(str6);      //输出:********n*****Czhenya            //======================重复描述字符=================            //示例:校验输入内容是否为合法QQ号(备注:QQ号为5 - 12位数字)            string qq1 = "1021570284";            string qq2 = "1009461014123546";            string str7 = @"^\d{5,12}$";      //误区:str7 = @"\d{5,12}";              Console.WriteLine(Regex.IsMatch(qq1,str7));  //输出:Ture;            Console.WriteLine(Regex.IsMatch(qq2, str7)); //输出:False            //========================择一匹配======================            //示例:查找数字或字母            string str8 = "wqjida213暗示法尽快哈就dsa12";            string str9 = @"\d|[a-z]";            MatchCollection cols = Regex.Matches(str8, str9);            for (int i = 0; i < cols.Count; i++)            {                Console.WriteLine(cols[i]);            }            // 输出:wqjida213dsa12            //示例:校验国内电话号码(支持三种写法校验 A. 010 - 87654321 B. (010)87654321 C.01087654321 D.010 87654321)            string str10 = @"\(0\d{2,3}\)[- ]?\d{7,8}|^0\d{2,3}[- ]?\d{7,8}$";            //匹配IP地址的正则表达式   (0-255.0-255.0-255.0-255)            string str11 = @"^(((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?))$";            Console.ReadKey();        }    }}

以上程序运行结果图:(输入是312123)
结果图

原创粉丝点击