C#正则表达式。

什么是正则表达式:

正则表达式是用来进行文本处理的技术,是语言无关的。

是由普通字符和特殊字符组成的文字模式,用来描述字符串的特征。

元字符:

1、  .    :  除 \n 以外的任意的单个字符。   加\ 可以转义

a.b----- 表示 a  b  之间可以有除了  \n   的任意字符。

2、 [ ]   :取其中一个字符(必须)。  [  a -z ]    [  a-zA-z  ] ,点出现在里面表示一个普通点。

3、|   : 或   z|food      z或food    (z)|(f)ood     z 或f     ood  

4、()  表示优先级    或  表示提取组

5、*   :  a.*b       * 表示前面的表达式出现0次或多次。

6、+ : a.+b     + 表示前面的表达式出现一次或多次 

7、? : a.?b     ?表示前面的表达式出现0次或1次。 

8、{}  : [0-9] {8}    固定次数    [0-9] {4,7}  最少  最多

9、^   :  ^ a     必须以a开头。

10、 $   :   $a  以a结尾。

11、[  ^a ]  :   不能有a的一个字符

12、 \d  : 0-9      加@    

13、|D   :除0-9

14、\w : 所有英文数字单字符。汉字也行  下划线 

15、\W  :  除了。。。。

16、\s  : 表示所有不可见字符。  \S  :   除了。。。。。

使用正则表达式。

    class Program
    {
        static void Main(string[] args)
        {
            //Regex.IsMatch(); //判断给定的字符串是否匹配某个正则表达式。
            //Regex.Match();  //用来从给定的字符串中按照正则表达式的要求提取【一个】匹配的字符串。
            //Regex.Matches();  //用来从给定的字符串中按照正则表达式的要求提取【所有】匹配的字符串。
            // Regex.Replace();  //替换所有正则表达式匹配的字符串为另外一个字符串。
            while (true)
            {
                Console.WriteLine("输入一个字符串");
                string str = Console.ReadLine();
                //验证给定的字符串是否为合法的邮政编码。
                //要想完全匹配,必须加 ^  和   $   .  否则只要含有就返回True
                bool b = Regex.IsMatch(str, "^[0-9]{6}$");
                Console.WriteLine(b);
            }
        }
    }
    }

字符串提取:

 class Program
    {
        static void Main(string[] args)
        {
            //Regex.IsMatch(); //判断给定的字符串是否匹配某个正则表达式。
            //Regex.Match();  //用来从给定的字符串中按照正则表达式的要求提取【一个】匹配的字符串。
            //Regex.Matches();  //用来从给定的字符串中按照正则表达式的要求提取【所有】匹配的字符串。
            // Regex.Replace();  //替换所有正则表达式匹配的字符串为另外一个字符串。

            //提取字符串中所有匹配项。
            string str = "ss  23  哈哈  2233收索 66aa12";
            MatchCollection matches= Regex.Matches(str, "[0-9]+");
            foreach (var  item in matches)
            {
                Console.WriteLine(item);
            }

        }
    }

 贪婪模式与非贪婪模式。

贪婪:.+    :  默认为贪婪模式,尽可能多的匹配。

非贪婪: .+?  : 尽可能少的匹配(1个)  在限定符后使用  ?  表示终止贪婪模式。

class Program
    {
        static void Main(string[] args)
        {
            //Regex.IsMatch(); //判断给定的字符串是否匹配某个正则表达式。
            //Regex.Match();  //用来从给定的字符串中按照正则表达式的要求提取【一个】匹配的字符串。
            //Regex.Matches();  //用来从给定的字符串中按照正则表达式的要求提取【所有】匹配的字符串。
            // Regex.Replace();  //替换所有正则表达式匹配的字符串为另外一个字符串。
            string msg = "大家好,我是杨幂。我是张靓颖。我是刘德华。";
            MatchCollection matches = Regex.Matches(msg, "我是.*?。");
            foreach (var item in matches)
            {
                Console.WriteLine(item);
            }
           
        }
    }

提取网页上的邮箱。

class Program
    {
        static void Main(string[] args)
        {
            //1、下载字符串。
            WebClient client = new WebClient();
            string html = client.DownloadString("file:///C:/Users//Desktop/1.html");
            //从字符串中提取邮件地址。
            MatchCollection matches = Regex.Matches(html, @"[a-zA-Z0-9]+@[a-zA-Z0-9.]+");
            foreach (Match item in matches)
            {
                Console.WriteLine(item.Value);
            }
            Console.WriteLine("一共{0}个邮箱地址",matches.Count);
        }
    }

 正则替换:

Regex.Replace()

class Program
    {
        static void Main(string[] args)
        {
            string msg = "你aaa好aa啊aaaaa";
            msg = Regex.Replace(msg, "a+", "A");
            Console.WriteLine(msg);
        }
    }

替换组实现:

 class Program
    {
        static void Main(string[] args)
        {
            //把 ' ' 里的内容替换为 【 】
            string msg = "hello 'welcom' to 'china'";
            msg = Regex.Replace(msg, "'(.+?)'", "【$1】");
            Console.WriteLine(msg);
        }
    }

隐藏手机号:

class Program
    {
        static void Main(string[] args)
        {
            string msg = "张玉昊 15983531955 胡云钰 15008306525";
            msg = Regex.Replace(msg, "([0-9]{3})[0-9]{4}([0-9]{4})", "$1****$2");
            Console.WriteLine(msg);
        }
    }

\b  的使用   判断是否为一个单词   \bdog\b  --- 是不是dog这个单词。

提取出所有三个字母的单词:

static void Main(string[] args)
        {

            string str = "this is a dog, it's not a cat.";
            MatchCollection matches= Regex.Matches(str, @"\b[a-zA-Z]{3}\b");
            foreach (Match item in matches)
            {
                Console.WriteLine(item.Value);
            }
        }

反向引用。 

反向引用使用  \    替换引用用  $

输出  我喜欢你

class Program
    {
        static void Main(string[] args)
        {

            string str = "我我我喜喜欢你你";
            str = Regex.Replace(str, @"(.)\1+","$1");
            Console.WriteLine(str);
        }
    }

 

posted @ 2019-03-18 21:30  张玉昊  阅读(361)  评论(0编辑  收藏  举报