正则表达式入门与进阶

正则表达式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string strInput, strPattern, strResult, strReplaced;

            //1、验证电话
            //  【^】表示匹配开始,【$】表示匹配结束
            //  【\d{3,4}-】示例:“111-”,“1111-”。
            //  【?】表示前面的子表达式零次或一次,等价于{0,1}
            strInput = "111-123456";
            strPattern = @"^(\d{3,4}-)?\d{6,8}$";
            if (Regex.IsMatch(strInput, strPattern))
            {
                Console.WriteLine(strInput + "匹配\t" + strPattern + "\n");
            }
            //拓展:验证手机号码:@"^[1][3-5]\d{9}$")


            //2、验证密码
            //  【[A-Za-z]】表示匹配包含的任意字符:a到z,A到Z(必须要匹配至少一个)
            //  【+】表示匹配1或多个正好在它之前的那个字符如:"9+",匹配“98”,"899"
            strInput = "abc11c1";
            strPattern = @"[A-Za-z]+[0-9]";
            if (Regex.IsMatch(strInput, strPattern))
            {
                Console.WriteLine(strInput + "匹配\t" + strPattern + "\n");
            }

            //3、验证邮政编码
            //  【\d{6}】表示有且仅有6位数字
            strInput = "123456";
            strPattern = @"\d{6}";
            if (Regex.IsMatch(strInput, strPattern))
            {
                Console.WriteLine(strInput + "匹配\t" + strPattern + "\n");
            }

            //4、验证身份证号码
            //  【|】表示或者关系,如“a|b”匹配“accc”、“baww”
            //  【+】表示匹配1或多个正好在它之前的那个字符如:"9+",匹配“98”,"899"
            strInput = "123456789123456";
            strPattern = @"(^\d{18}$)|(^\d{15}$)";
            if (Regex.IsMatch(strInput, strPattern))
            {
                Console.WriteLine(strInput + "匹配\t" + strPattern + "\n");
            }

            //一些基础练习:
            //@"^[0-9]+\.[0-9]{2}$"  必须保留两位小数:如3214.12
            //@"^(0?[1-9]|1[0-2])$" 匹配1到12月份
            //@"^((0?[1-9])|((1|2)[0-9])|30|31)$"  匹配天数1到31
            //@"^[0-9]*$"      匹配数字(备注:【*】表示匹配前面的子表达式任意次,例"1ab5*"匹配1ab、1ab555等)
            //@"^[A-Z]+$"      匹配大写字母最少1次
            //@"^[A-Za-z]+$"   匹配字母
            //@"^[\u4e00-\u9fa5]{1,}$" 、@"^[\u4e00-\u9fff]{1,}$"    都可以匹配汉字,(unicode编码,好难记( ╯□╰ ))



            //下面来几个比较难滴
            //4、匹配两个连续且中间有空格(空白)的单词
            //  【\b】表示单词的边界,也就是指单词与空格之间的位置
            //  【(?\name子表达式)】将匹配的子表达式捕获到一个名称name中,那么不能包含任何字符表标点,不能以数字开头。可以使用单引号替代尖括号。
            //  【\w】表示包括下划线的任何单词字符,类似但不等价于"[A-Za-z0-9_]"
            //  【\s】匹配任何不可见字符,如空格,制表符,换页符等。等价于"[\f\n\r\t\v]"
            //  【\k<name>】命名后向引用,可以使用单引号替代尖括号。如"(?<name>\w)\k<name>" 匹配连续的两个重复字符如"aa"、"bb"。
            strInput = "The the quick brown for for jumped over the lazy dog dog";
            strPattern = @"\b(?<word>\w+)(\s+)(\k<word>)\b";
            MatchCollection matches = Regex.Matches(strInput, strPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Console.WriteLine("需检测重复的字符串" + strInput);
            if (matches.Count != 0)											    //如果集合中有内容
            {
                foreach (Match match in matches)
                {
                    string word = match.Groups["word"].Value;
                    Console.WriteLine("重复出现的单词:" + word);
                }
            }
            Console.WriteLine("");


            //5、使用正则表达式替换字符串
            //关键词:Regex.Replace
            strInput = "The the quick brown for for jumped over the lazy dog dog";
            strPattern = @"\b(?<word>)(\s+)(\k<word>)\b";
            strReplaced = "1";
            strResult = Regex.Replace(strInput, @"[A-Za-z]\*?", strReplaced);
            Console.WriteLine("替换前字符:\t" + strInput);
            Console.WriteLine("匹配\t" + strPattern + "\t的替换为:" + strReplaced);
            Console.WriteLine("替换后的字符:\t" + strResult + "\n");


            //6、使用正则表达式根据数字进行拆分
            //关键词:Regex.Split
            strInput = "The1quick1dog";
            strPattern = @"[1-9]";
            strResult = "";
            Console.WriteLine("拆分前的字符串:\t" + strInput);
            Console.WriteLine("正则表达式:\t" + strPattern);
            Console.WriteLine("拆分后的的字符串数组:");
            string[] P_Str = Regex.Split(strInput, strPattern);
            for (int i = 0; i < P_Str.Length; i++)
            {
                strResult += "P_Str[" + i + "]:" + P_Str[i] + "\t";
            }
            Console.WriteLine(strResult + "\n");


            //6、使用正则表达式匹配邮箱
            strInput = "1318@11.com";
            strPattern = @"^((\w+\.?)+)@([a-zA-Z]{2,4}|[0-9]{1,3})(\.[a-zA-Z]{2,4})$";
            strResult = "";
            Console.WriteLine("输入的邮箱字符串:\t" + strInput);
            if (Regex.IsMatch(strInput, strPattern))
            {
                Console.WriteLine(strInput + "匹配\t" + strPattern + "\n");
            }

            //7、使用正则表达式匹配id地址
            Console.WriteLine("ip地址匹配");
            string num = @"(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)";
            strInput = "251.168.0.1";
            strPattern = ("^" + num + "\\." + num + "\\." + num + "\\." + num + "$");
            if (Regex.IsMatch(strInput, strPattern))
            {
                Console.WriteLine(strInput + "某一段网址的正则:\t" + num + "\n");
            }

            //6、使用正则表达式匹配url地址
            strInput = "http://localhost/GYYSMS/BST.Biz.Safe.Presentation.Web/DangerousMonitor_oper.aspx?pMode=Add&pId=";
            strPattern = @"http(s)?://([\w-]\.)+[\w-]+(/[\w-./?%&=]*)?";
            strResult = "";
            Console.WriteLine("输入的url字符串:\t" + strInput);
            Console.WriteLine("正则表达式:\t" + strPattern);
            if (Regex.IsMatch(strInput, strPattern))
            {
                Console.WriteLine(strInput + "匹配\t" + strPattern + "\n");
            }
            Console.ReadLine();
        }
    }
}

运行结果

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值